java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot统一返回json格式并配置系统异常拦截

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

作者: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);;
		
		
	}

}

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文