springboot unified return json data format and configure the exception interception

This link: https: //blog.csdn.net/syystx/article/details/82870217
when the end is usually carried out before and after the separation of development we need to define a unified json format data exchange and system unhandled exception processing. The following detailed description of the implementation process springboot uniform exception handling framework for the section may be implemented by the code, the background and when the interface is not uniform format feedback type can be repackaged into a uniform format returned.

Specific achieve the following:

1, the definition of a unified format to return

public class RtnMsg{

private String rtnCode;

private String rtnMsg="";

private Object msg;

public RtnMsg(String rtnCode,String rtnMsg,Object msg){
this.rtnCode = rtnCode;
this.rtnMsg = rtnMsg;
this.msg = msg;
}

public RtnMsg(String rtnCode,String rtnMsg){
this.rtnCode = rtnCode;
this.rtnMsg = rtnMsg;
}

public RtnMsg(){
}

public String getRtnCode() {
return rtnCode;
}

public void setRtnCode(String rtnCode) {
this.rtnCode = rtnCode;
}

public String getRtnMsg() {
return rtnMsg;
}

public void setRtnMsg(String rtnMsg) {
= rtnMsg this.rtnMsg;
}

public a getMsg Object () {
return MSG;
}

public void setMsg (Object MSG) {
this.msg MSG =;
}




}
2, set common error code

RtnCode class {public

// return the normal
public static String STATUS_OK A Final = "000";
// Parameter error
public static String STATUS_PARAM Final = "001";
// Interface No
public static String STATUS_NOFOUND Final = "404";
// Capture abnormality
public static String STATUS_SYSERROR Final = "500";
}
. 3, defined uniform unhandled exception interception

org.springframework.web.bind.annotation.ControllerAdvice Import;
Import org.springframework.web.bind.annotation.ExceptionHandler;
Import org.springframework.web.bind.annotation.ResponseBody;
/ **
* @author suntongxin
* 2017 ON the Create on the afternoon of 12 December 1:55:12
* All right Reserved
* /
@ControllerAdvice
public class CommExceptionHandler {

@ResponseBody
@ExceptionHandler (value = Exception.class)
public RtnMsg handle (Exception E) {
RtnMsg msg = new new RtnMsg (RtnCode. STATUS_SYSERROR, "system abnormality, abnormality cause:" + e.getMessage ());
return MSG;
}
. 4, the injection response bean object intercepting

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author suntongxin
* Create on 2017年12月12日下午1:55:27
* All right reserved
*/
@Configuration
public class RtnMsgConfig{

@Bean
public ResponseBodyWrapFactoryBean getResponseBodyWrap(){

return new ResponseBodyWrapFactoryBean();

}


}
5、设置bean过滤原则

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;
/**
* @author suntongxin
* Create on 2017年12月12日上午10:48:43
* All right reserved
*/
public class ResponseBodyWrapFactoryBean implements InitializingBean{

@Autowired
private RequestMappingHandlerAdapter adapter;

@Override
public void afterPropertiesSet() throws Exception {

List<HandlerMethodReturnValueHandler> returnValueHandlers = adapter.getReturnValueHandlers();
List<HandlerMethodReturnValueHandler> handlers = new ArrayList(returnValueHandlers);
decorateHandlers(handlers);
adapter.setReturnValueHandlers(handlers);


}


private void decorateHandlers(List<HandlerMethodReturnValueHandler> handlers){

for(HandlerMethodReturnValueHandler handler : handlers){
if(handler instanceof RequestResponseBodyMethodProcessor){
ResponseBodyWrapHandler decorator = new ResponseBodyWrapHandler(handler);
int index = handlers.indexOf(handler);
handlers.set(index, decorator);
break;
}
}

}

}
6, particularly to achieve uniform processing returns json

package cn.seisys.common;

import org.springframework.core.MethodParameter;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
/**
* @author suntongxin
* Create on 2017年12月12日上午10:48:54
* All right reserved
*/
public class ResponseBodyWrapHandler implements HandlerMethodReturnValueHandler{

private final HandlerMethodReturnValueHandler delegate;

public ResponseBodyWrapHandler(HandlerMethodReturnValueHandler delegate){

this.delegate = delegate;

}

@Override
Boolean supportsReturnType public (MethodParameter the returnType) {

return delegate.supportsReturnType (the returnType);

}

@Override
public void handleReturnValue (the returnValue Object, the returnType MethodParameter, ModelAndViewContainer mavContainer,
NativeWebRequest WebRequest) throws Exception {
RtnMsg rtnMsg = null;
IF (the instanceof the returnValue RtnMsg) {
= rtnMsg (rtnMsg) the returnValue;
} the else {
rtnMsg = new new rtnMsg (RtnCode.STATUS_OK, "", the returnValue);
}

delegate.handleReturnValue (rtnMsg, the returnType, mavContainer, WebRequest) ;;


}

}
 
-------- --------
Disclaimer: this article is CSDN blogger "L If the child 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/syystx/article/details/82870217

Guess you like

Origin www.cnblogs.com/qq75077027/p/11517943.html