本文連接:https://blog.csdn.net/syystx/article/details/82870217
一般進行先後端分離開發時咱們須要定義統一的json數據交互格式並對系統未處理異常進行處理。如下具體介紹在springboot中的實現過程,經過該章節代碼可實現框架統一異常處理,並當後臺接口反饋類型不爲統一格式時可以進行從新包裝成統一格式進行返回。java
具體實現以下:web
一、定義統一返回格式spring
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) {
this.rtnMsg = rtnMsg;
}
public Object getMsg() {
return msg;
}
public void setMsg(Object msg) {
this.msg = msg;
}
}
二、設置經常使用錯誤碼json
public class RtnCode {
//正常返回
public static final String STATUS_OK = "000";
//參數錯誤
public static final String STATUS_PARAM = "001";
//接口未發現
public static final String STATUS_NOFOUND = "404";
//捕獲到異常
public static final String STATUS_SYSERROR = "500";
}
三、定義未處理異常統一攔截後端
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* @author suntongxin
* Create on 2017年12月12日下午1:55:12
* All right reserved
*/
@ControllerAdvice
public class CommExceptionHandler {
@ResponseBody
@ExceptionHandler(value = Exception.class)
public RtnMsg handle(Exception e){
RtnMsg msg = new RtnMsg(RtnCode.STATUS_SYSERROR, "系統異常,異常緣由:"+e.getMessage());
return msg;
}
四、注入攔截response的bean對象springboot
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();
}
}
五、設置bean過濾原則mvc
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;
}
}
}
}
六、實現具體的統一json返回處理app
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
public boolean supportsReturnType(MethodParameter returnType) {
return delegate.supportsReturnType(returnType);
}
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest) throws Exception {
RtnMsg rtnMsg = null;
if(returnValue instanceof RtnMsg){
rtnMsg = (RtnMsg)returnValue;
}else{
rtnMsg = new RtnMsg(RtnCode.STATUS_OK,"",returnValue);
}
delegate.handleReturnValue(rtnMsg, returnType, mavContainer, webRequest);;
}
}
————————————————
版權聲明:本文爲CSDN博主「L若兒」的原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接及本聲明。
原文連接:https://blog.csdn.net/syystx/article/details/82870217框架