Spring?Boot統(tǒng)一接口返回及全局異常處理
前言:
前段時間接手了一個老項目,現(xiàn)在需要在此項目中添加一些新的需求,同事在開發(fā)過程中遇到了一些問題?
- 1.成功的狀態(tài)到底是200還是0啊,訂單系統(tǒng)200代表成功,而會員系統(tǒng)卻是0代表成功。
- 2.接口返回的結果中,有些是用msg字段表示描述,有些又是用desc字段描述,前段處理起來比較麻煩能不能統(tǒng)一。
- 3.錯誤提示信息需要支持國際化。
其實這些問題,歸根究底還是代碼規(guī)范問題,我們需要將接口定義和全局異常統(tǒng)一處理,歷史項目10多個工程,難道每個工程都去實現(xiàn)一遍,答案可定是不可能的。
1、解決方案

定義公共模塊,實現(xiàn)統(tǒng)一接口定義規(guī)范和異常處理,其他的系統(tǒng)進行依賴和擴展即可。
2、具體實現(xiàn)
2.1 定義狀態(tài)碼統(tǒng)一接口
public interface BaseResultCode
{
/**
* 狀態(tài)碼
* @return
*/
int getCode();
/**
* 提示信息
* @return
*/
String getMsg();
}2.2 公共模塊狀態(tài)碼枚舉類
public enum ResultCode implements BaseResultCode
{
OK(200, "成功"),
ERROR(300,"系統(tǒng)異常"),
NEED_AUTH(301, "非法請求,請重新登錄"),
PARAMTER_ERROR(302, "參數(shù)錯誤");
//省略其他定義錯誤碼
private int code;
private String msg;
private ResultCode(int code, String msg)
{
this.code = code;
this.msg = msg;
}
public static ResultCode getValue(int code)
{
for (ResultCode errorCode : values())
{
if (errorCode.getCode() == code)
{
return errorCode;
}
}
return null;
}
//省略Get、Set方法
}2.3 定義全局自定義異常
public class SysException extends RuntimeException
{
private static final long serialVersionUID = 5225171867523879342L;
private int code;
private String msg;
private Object[] params;
private BaseResultCode errorCode;
public SysException()
{
super();
}
public SysException(String message)
{
super(message);
}
public SysException(Throwable cause)
{
super(cause);
}
public SysException(int code ,String message)
{
this.code = code;
this.msg = message;
}
public SysException(int code ,String message, Object[] params)
{
this(code, message);
this.params= params;
}
public SysException(String message, Throwable cause)
{
super(message, cause);
}
public SysException(BaseResultCode errorCode)
{
this.errorCode = errorCode;
}
public SysException(String message, Object[] params)
{
super(message);
this.params = params;
}
public SysException(BaseResultCode errorCode, String message, Object[] params)
{
this(message, params);
this.errorCode = errorCode;
}
/**
* Construct by default
*
* @param message
* message
* @param parameters
* parameters
* @param cause
* cause
*/
public SysException(String message, Object[] params, Throwable cause)
{
super(message, cause);
this.params = params;
}
public int getCode()
{
return code;
}
public void setCode(int code)
{
this.code = code;
}
public String getMsg()
{
return msg;
}
public void setMsg(String msg)
{
this.msg = msg;
}
/**
* @return the params
*/
public Object[] getParams()
{
return params;
}
/**
* @param params
* the params to set
*/
public void setParams(Object[] params)
{
this.params = params;
}
public BaseResultCode getErrorCode()
{
return errorCode;
}
public void setErrorCode(BaseResultCode errorCode)
{
this.errorCode = errorCode;
}
}2.4 定義統(tǒng)一接口格式輸出類
public class Result implements Serializable
{
private static final long serialVersionUID = -1773941471021475043L;
private Object data;
private int code;
private String msg;
public Result()
{
}
public Result(int code, Object data, String msg)
{
this.code = code;
this.data = data;
this.msg = msg;
}
public Result(int code, String desc)
{
this(code, null, desc);
}
public Result(BaseResultCode errorCode)
{
this(errorCode.getCode(), null, errorCode.getMsg());
}
public static Result success()
{
return success(null);
}
public static Result success(Object data)
{
Result result = new Result();
result.setData(data);
result.setCode(ResultCode.OK.getCode());
return result;
}
public static Result error(String msg)
{
Result result = new Result();
result.setCode(ResultCode.ERROR.getCode());
result.setMsg(msg);
return result;
}
public static Result error(BaseResultCode baseCode)
{
Result result = new Result();
result.setCode(baseCode.getCode());
result.setMsg(baseCode.getMsg());
return result;
}
}個人建議:統(tǒng)一接口輸出類不要定義為泛型類型
2.5 定義統(tǒng)一接口格式輸出類
@RestControllerAdvice
public class SysExceptionHandler
{
public static Log logger = LogManager.getLogger(SysExceptionHandler.class);
@ExceptionHandler(Exception.class)
public Result handleException(HttpServletRequest request,
Exception ex)
{
logger.error("Handle Exception Request Url:{},Exception:{}",request.getRequestURL(),ex);
Result result = new Result();
//系統(tǒng)異常
if (ex instanceof SysException)
{
SysException se = (SysException) ex;
BaseResultCode resultCode =se.getErrorCode();
if(resultCode==null)
{
result = Result.error(se.getMessage());
}
else
{
result = new Result(resultCode.getCode(),
StringUtil.isNotEmpty(se.getMessage())?se.getMessage():resultCode.getMsg());
}
}
//參數(shù)錯誤
else if (ex instanceof ConstraintViolationException)
{
ConstraintViolationException v = (ConstraintViolationException) ex;
String message = v.getConstraintViolations().iterator().next()
.getMessage();
result.setCode(ResultCode.PARAMTER_ERROR.getCode());
result.setMsg(ResultCode.PARAMTER_ERROR.getMsg() + ":" + message);
}
//參數(shù)錯誤
else if (ex instanceof BindException)
{
BindException v = (BindException) ex;
String message = v.getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(","));
result.setCode(ResultCode.PARAMTER_ERROR.getCode());
result.setMsg(ResultCode.PARAMTER_ERROR.getMsg() + ":" + message);
}
//參數(shù)錯誤
else if (ex instanceof MethodArgumentNotValidException)
{
MethodArgumentNotValidException v = (MethodArgumentNotValidException) ex;
String message = v.getBindingResult().getAllErrors().stream().map(ObjectError::getDefaultMessage).collect(Collectors.joining(","));
result.setCode(ResultCode.PARAMTER_ERROR.getCode());
result.setMsg(ResultCode.PARAMTER_ERROR.getMsg() + ":" + message);
}
else
{
result = new Result(ResultCode.ERROR.getCode(),ExceptionUtil.getErrorMsg(ex));
}
logger.info("exception handle reuslt:" + result);
return result;
}
}上述定義已經(jīng)可以實現(xiàn)全局接口和異常的統(tǒng)一處理,但是存在的如下問題
每個controller都需要返回Reesult類型,且每個方法都需要返回Result.success()或者Result.success(data)的結果,有點重復,需要進行優(yōu)化。
@GetMapping("addUser")
public Result add()
{
for(int i=0;i<10;i++)
{
TUser user = new TUser();
//user.setOid(IdWorker.getId());
user.setName("shareing_"+i);
user.setAge(i);
userService.addUser(user);
}
return Result.success();
}2.6 接口統(tǒng)一輸出優(yōu)化
實現(xiàn)方式只需要實現(xiàn)ResponseBodyAdvice接口,重寫beforeBodyWrite方法接口。
@RestControllerAdvice
public class ResponseAdvice implements ResponseBodyAdvice<Object>
{
private Logger logger = LoggerFactory.getLogger(ResponseAdvice.class);
@Override
public boolean supports(MethodParameter returnType,
Class<? extends HttpMessageConverter<?>> converterType)
{
return true;
}
@Override
public Object beforeBodyWrite(Object o, MethodParameter returnType,
MediaType selectedContentType,
Class<? extends HttpMessageConverter<?>> selectedConverterType,
ServerHttpRequest request, ServerHttpResponse response)
{
logger.info("before body write param:{}",o);
if(o instanceof String)
{
//序列化結果輸出
return FastJsonUtil.toJSONString(Result.success(o));
}
else if (o instanceof Result)
{
return o;
}
return Result.success(o);
}
}經(jīng)過優(yōu)化后,controller輸出可以根據(jù)業(yè)務的需求定義輸出對象。
@GetMapping("getUserByName")
public TUser getUserByName1(@RequestParam String name)
{
logger.info("getUserByName paramter name:"+name);
return userService.getUserByName(name);
}2.7 子系統(tǒng)如何實現(xiàn)
子系統(tǒng)引入common的jar包,
<dependency> <groupId>com.xx</groupId> <artifactId>xx-common</artifactId> <version>2.0</version> </dependency>
3、子系統(tǒng)定義狀態(tài)碼,實現(xiàn)BaseResultCode接口
public enum OrderModelErrorCode implements BaseResultCode
{
ORDER_STATUS_ERROR(1000, "訂單狀態(tài)不正確");
private int code;
private String msg;
private UserModelErrorCode(int code, String msg)
{
this.code = code;
this.msg = msg;
}
@Override
public int getCode()
{
return code;
}
@Override
public String getMsg()
{
return msg;
}
}定義異常處理類,繼承公共異常處理類SysExceptionHandler
@RestControllerAdvice
public class OrderModalExceptionHandle extends SysExceptionHandler
{
@Override
public Result handleException(HttpServletRequest request, Exception ex)
{
return super.handleException(request, ex);
//子系統(tǒng)可以擴展異常處理
}
}子系統(tǒng)使用示例:
@Override
public Order getOrder(String orderId)
{
Order order =getOrder(orderId);
//相關偽代碼
if(order.getStatus()>120)
{
throw new SysException(OrderModelErrorCode.ORDER_STATUS_ERROR);
}
return order;
}
經(jīng)過相關項目的重構,已經(jīng)解決了第一個和第二問題,關于第三個國際化問題,將在后續(xù)的文章中講解。
到此這篇關于Spring Boot統(tǒng)一接口返回以及全局異常處理的文章就介紹到這了,更多相關Spring Boot異常處理內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于spring AOP @Around @Before @After的區(qū)別說明
這篇文章主要介紹了基于spring AOP @Around @Before @After的區(qū)別說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Java中l(wèi)ength,length(),size()詳解及區(qū)別
這篇文章主要介紹了Java中l(wèi)ength,length(),size()詳解及區(qū)別的相關資料,需要的朋友可以參考下2016-11-11
mybatis-plus-boot-starter包與mybatis-plus-generator的沖突解決
本文主要介紹了mybatis-plus-boot-starter包與mybatis-plus-generator的沖突解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-10-10
SpringBoot如何對LocalDateTime進行格式化并解析
這篇文章主要介紹了SpringBoot如何對LocalDateTime進行格式化方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07
SpringCloud啟動eureka server后,沒報錯卻不能訪問管理頁面(404問題)
這篇文章主要介紹了SpringCloud啟動eureka server后,沒報錯卻不能訪問管理頁面(404問題),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11

