只需一步,在Spring Boot中統(tǒng)一Restful API返回值格式與統(tǒng)一處理異常
統(tǒng)一返回值
在前后端分離大行其道的今天,有一個(gè)統(tǒng)一的返回值格式不僅能使我們的接口看起來更漂亮,而且還可以使前端可以統(tǒng)一處理很多東西,避免很多問題的產(chǎn)生。
比較通用的返回值格式如下:
- public class Result<T> {
- // 接口調(diào)用成功或者失敗
- private Integer code = 0;
- // 失敗的具體code
- private String errorCode = "";
- // 需要傳遞的信息,例如錯(cuò)誤信息
- private String msg;
- // 需要傳遞的數(shù)據(jù)
- private T data;
- ...
- }
最原始的接口如下:
- @GetMapping("/test")
- public User test() {
- return new User();
- }
當(dāng)我們需要統(tǒng)一返回值時(shí),可能會使用這樣一個(gè)辦法:
- @GetMapping("/test")
- public Result test() {
- return Result.success(new User());
- }
這個(gè)方法確實(shí)達(dá)到了統(tǒng)一接口返回值的目的,但是卻有幾個(gè)新問題誕生了:
- 接口返回值不明顯,不能一眼看出來該接口的返回值。
- 每一個(gè)接口都需要增加額外的代碼量。
所幸Spring Boot已經(jīng)為我們提供了更好的解決辦法,只需要在項(xiàng)目中加上以下代碼,就可以無感知的為我們統(tǒng)一全局返回值。
- /**
- * 全局返回值統(tǒng)一封裝
- */
- @EnableWebMvc
- @Configuration
- public class GlobalReturnConfig {
- @RestControllerAdvice
- static class ResultResponseAdvice implements ResponseBodyAdvice<Object> {
- @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)一封裝時(shí)我們沒有考慮當(dāng)接口拋出異常的情況。當(dāng)接口拋出異常時(shí)讓用戶直接看到服務(wù)端的異??隙ㄊ遣粔蛴押玫?,而我們也不可能每一個(gè)接口都去try/catch進(jìn)行處理,此時(shí)只需要使用@ExceptionHandler注解即可無感知的全局統(tǒng)一處理異常。
- @RestControllerAdvice
- public 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ù)中需要拋出錯(cuò)誤時(shí),可以手動(dòng)拋出AlertException。
以上就是統(tǒng)一處理返回值和統(tǒng)一處理異常的兩步。