springboot统一返回json数据格式并配置系统异常拦截

Posted qq75077027

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot统一返回json数据格式并配置系统异常拦截相关的知识,希望对你有一定的参考价值。

本文链接: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

以上是关于springboot统一返回json数据格式并配置系统异常拦截的主要内容,如果未能解决你的问题,请参考以下文章

教大家用 Springboot 返回 Json 数据及统一数据封装

SpringBoot返回统一的JSON标准格式

SpringBoot返回统一的JSON标准格式

springBoot项目设置统一响应返回

SpringBoot入门系列如何返回统一的数据格式

springboot 定义统一的返回json渲染格式