Spring Boot中统一Restful API返回值格式与统一处理异常

2019-08-2216:50:37后端程序开发Comments2,433 views字数 2075阅读模式

统一返回值

在前后端分离大行其道的今天,有一个统一的返回值格式不仅能使我们的接口看起来更漂亮,而且还可以使前端可以统一处理很多东西,避免很多问题的产生。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15758.html

比较通用的返回值格式如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15758.html

public class Result<T> {
    // 接口调用成功或者失败
    private Integer code = 0;
    // 失败的具体code
    private String errorCode = "";
    // 需要传递的信息,例如错误信息
    private String msg;
    // 需要传递的数据
    private T data;
    ...
}

最原始的接口如下:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15758.html

    @GetMapping("/test")
    public User test() {
        return new User();
    }

当我们需要统一返回值时,可能会使用这样一个办法:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15758.html

    @GetMapping("/test")
    public Result test() {
        return Result.success(new User());
    }

这个方法确实达到了统一接口返回值的目的,但是却有几个新问题诞生了:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15758.html

  • 接口返回值不明显,不能一眼看出来该接口的返回值。
  • 每一个接口都需要增加额外的代码量。

所幸Spring Boot已经为我们提供了更好的解决办法,只需要在项目中加上以下代码,就可以无感知的为我们统一全局返回值。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15758.html

/**
 * 全局返回值统一封装
 */
@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);
        }
    }
}

Spring Boot中统一Restful API返回值格式与统一处理异常
而我们的接口只需要写成最原始的样子就行了。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15758.html

    @GetMapping("/test")
    public User test() {
        return new User();
    }

统一处理异常

将返回值统一封装时我们没有考虑当接口抛出异常的情况。当接口抛出异常时让用户直接看到服务端的异常肯定是不够友好的,而我们也不可能每一个接口都去try/catch进行处理,此时只需要使用@ExceptionHandler注解即可无感知的全局统一处理异常。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15758.html

@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) {//预定义异常
                return new Result(((AlertException) e).getRetCode());
            } else {
                return new Result(1, e.getMessage() != null ? e.getMessage() : "");
            }
        } else {//其它异常
            if (Util.isProduct()) {//如果是正式环境,统一提示
                return new Result(RetCode.ERROR);
            } else {//测试环境,alert异常信息
                return new Result(1, StringUtils.isNotBlank(e.getMessage()) ? e.getMessage() : e.toString());
            }
        }
    }

}

其中的AlertException为我们自定义的异常,因此当业务中需要抛出错误时,可以手动抛出AlertException文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15758.html

以上就是统一处理返回值和统一处理异常的两步。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/15758.html

  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/bc/15758.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定