javascript
无返回值_只需一步,在Spring Boot中统一Restful API返回值格式与处理异常
統(tǒng)一返回值
在前后端分離大行其道的今天,有一個統(tǒng)一的返回值格式不僅能使我們的接口看起來更漂亮,而且還可以使前端可以統(tǒng)一處理很多東西,避免很多問題的產(chǎn)生。
比較通用的返回值格式如下:
public class Result { // 接口調(diào)用成功或者失敗 private Integer code = 0; // 失敗的具體code private String errorCode = ""; // 需要傳遞的信息,例如錯誤信息 private String msg; // 需要傳遞的數(shù)據(jù) private T data; ...}最原始的接口如下:
@GetMapping("/test") public User test() { return new User(); }當(dāng)我們需要統(tǒng)一返回值時,可能會使用這樣一個辦法:
@GetMapping("/test") public Result test() { return Result.success(new User()); }這個方法確實達到了統(tǒng)一接口返回值的目的,但是卻有幾個新問題誕生了:
- 接口返回值不明顯,不能一眼看出來該接口的返回值。
- 每一個接口都需要增加額外的代碼量。
所幸Spring Boot已經(jīng)為我們提供了更好的解決辦法,只需要在項目中加上以下代碼,就可以無感知的為我們統(tǒng)一全局返回值。
/** * 全局返回值統(tǒng)一封裝 */@EnableWebMvc@Configurationpublic class GlobalReturnConfig { @RestControllerAdvice static class ResultResponseAdvice implements ResponseBodyAdvice { @Override public boolean supports(MethodParameter methodParameter, Class extends HttpMessageConverter>> aClass) { return true; } @Override public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class extends HttpMessageConverter>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { if (body instanceof Result) { return body; } return new Result(body); } }}而我們的接口只需要寫成最原始的樣子就行了。
@GetMapping("/test") public User test() { return new User(); }統(tǒng)一處理異常
將返回值統(tǒng)一封裝時我們沒有考慮當(dāng)接口拋出異常的情況。當(dāng)接口拋出異常時讓用戶直接看到服務(wù)端的異常肯定是不夠友好的,而我們也不可能每一個接口都去try/catch進行處理,此時只需要使用@ExceptionHandler注解即可無感知的全局統(tǒng)一處理異常。
@RestControllerAdvicepublic class GlobalExceptionHandler { private static final Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class); /** * 全局異常處理 */ @ExceptionHandler public JsonData handleException(HttpServletRequest request, HttpServletResponse response, final Exception e) { LOG.error(e.getMessage(), e); if (e instanceof AlertException) {//可以在前端Alert的異常 if (((AlertException) e).getRetCode() != null) {//預(yù)定義異常 return new Result(((AlertException) e).getRetCode()); } else { return new Result(1, e.getMessage() != null ? e.getMessage() : ""); } } else {//其它異常 if (Util.isProduct()) {//如果是正式環(huán)境,統(tǒng)一提示 return new Result(RetCode.ERROR); } else {//測試環(huán)境,alert異常信息 return new Result(1, StringUtils.isNotBlank(e.getMessage()) ? e.getMessage() : e.toString()); } } }}其中的AlertException為我們自定義的異常,因此當(dāng)業(yè)務(wù)中需要拋出錯誤時,可以手動拋出AlertException。
以上就是統(tǒng)一處理返回值和統(tǒng)一處理異常的兩步。
總結(jié)
以上是生活随笔為你收集整理的无返回值_只需一步,在Spring Boot中统一Restful API返回值格式与处理异常的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: adb 命令的个人记录
- 下一篇: spring的aop_Spring AO