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);;
}
}总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论