springboot统一返回json数据格式并配置系统异常拦截方式
更新时间:2024年08月09日 10:26:48 作者:L若儿
这篇文章主要介绍了springboot统一返回json数据格式并配置系统异常拦截方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
springboot统一返回json格式并配置系统异常拦截
通常进行前后端分离开发时我们需要定义统一的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);;
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot采用Dynamic-Datasource方式实现多JDBC数据源
在某些情况下,如果我们需要配置多个数据源,本文主要介绍了SpringBoot采用Dynamic-Datasource方式实现多JDBC数据源,具有一定的参考价值,感兴趣的可以了解一下2023-10-10
intellij idea如何将web项目打成war包的实现
这篇文章主要介绍了intellij idea如何将web项目打成war包的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2020-07-07
RestTemplate如何添加请求头headers和请求体body
这篇文章主要介绍了RestTemplate如何添加请求头headers和请求体body问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-07-07
面试题:java中为什么foreach中不允许对元素进行add和remove
读者遇到了一个比较经典的面试题,也就是标题上说的,为什么 foreach 中不允许对元素进行 add 和 remove,本文就详细的介绍一下,感兴趣的可以了解一下2021-10-10


最新评论