本文鏈接:https://blog.csdn.net/syystx/article/details/82870217
通常進行前后端分離開發時我們需要定義統一的json數據交互格式並對系統未處理異常進行處理。以下具體介紹在springboot中的實現過程,通過該章節代碼可實現框架統一異常處理,並當后台接口反饋類型不為統一格式時能夠進行重新包裝成統一格式進行返回。
具體實現如下:
1、定義統一返回格式
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;
}
}
2、設置常用錯誤碼
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";
}
3、定義未處理異常統一攔截
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;
}
4、注入攔截response的bean對象
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、實現具體的統一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
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