SpringBoot JSON API

无侵入式统一返回JSON格式

定义JSON格式

后端返回给前端一般情况下使用JSON格式, 定义如下

  1. {
  2. "code": 200,
  3. "message": "OK",
  4. "data": {
  5. }
  6. }
  • code: 返回状态码
  • message: 返回信息的描述
  • data: 返回值

    定义JavaBean字段

    定义状态码枚举类

    1. @ToString
    2. @Getter
    3. public enum ResultStatus {
    4. SUCCESS(HttpStatus.OK, 200, "OK"),
    5. BAD_REQUEST(HttpStatus.BAD_REQUEST, 400, "Bad Request"),
    6. INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, 500, "Internal Server Error"),;
    7. /** 返回的HTTP状态码, 符合http请求 */
    8. private HttpStatus httpStatus;
    9. /** 业务异常码 */
    10. private Integer code;
    11. /** 业务异常信息描述 */
    12. private String message;
    13. ResultStatus(HttpStatus httpStatus, Integer code, String message) {
    14. this.httpStatus = httpStatus;
    15. this.code = code;
    16. this.message = message;
    17. }
    18. }
    状态码和信息以及http状态码就能一一对应了便于维护。
    1. @Getter
    2. @ToString
    3. public class Result<T> {
    4. /** 业务错误码 */
    5. private Integer code;
    6. /** 信息描述 */
    7. private String message;
    8. /** 返回参数 */
    9. private T data;
    10. private Result(ResultStatus resultStatus, T data) {
    11. this.code = resultStatus.getCode();
    12. this.message = resultStatus.getMessage();
    13. this.data = data;
    14. }
    15. /** 业务成功返回业务代码和描述信息 */
    16. public static Result<Void> success() {
    17. return new Result<Void>(ResultStatus.SUCCESS, null);
    18. }
    19. /** 业务成功返回业务代码,描述和返回的参数 */
    20. public static <T> Result<T> success(T data) {
    21. return new Result<T>(ResultStatus.SUCCESS, data);
    22. }
    23. /** 业务成功返回业务代码,描述和返回的参数 */
    24. public static <T> Result<T> success(ResultStatus resultStatus, T data) {
    25. if (resultStatus == null) {
    26. return success(data);
    27. }
    28. return new Result<T>(resultStatus, data);
    29. }
    30. /** 业务异常返回业务代码和描述信息 */
    31. public static <T> Result<T> failure() {
    32. return new Result<T>(ResultStatus.INTERNAL_SERVER_ERROR, null);
    33. }
    34. /** 业务异常返回业务代码,描述和返回的参数 */
    35. public static <T> Result<T> failure(ResultStatus resultStatus) {
    36. return failure(resultStatus, null);
    37. }
    38. /** 业务异常返回业务代码,描述和返回的参数 */
    39. public static <T> Result<T> failure(ResultStatus resultStatus, T data) {
    40. if (resultStatus == null) {
    41. return new Result<T>(ResultStatus.INTERNAL_SERVER_ERROR, null);
    42. }
    43. return new Result<T>(resultStatus, data);
    44. }
    45. }
    因为使用构造方法进行创建对象太麻烦了,使用静态方法来创建对象这样简单明了

    Result实体返回测试

    1. @RestController
    2. @RequestMapping("/hello")
    3. public class HelloController {
    4. private static final HashMap<String, Object> INFO;
    5. static {
    6. INFO = new HashMap<>();
    7. INFO.put("name", "galaxy");
    8. INFO.put("age", "70");
    9. }
    10. @GetMapping("/hello")
    11. public Map<String, Object> hello() {
    12. return INFO;
    13. }
    14. @GetMapping("/result")
    15. @ResponseBody
    16. public Result<Map<String, Object>> helloResult() {
    17. return Result.success(INFO);
    18. }
    19. }
    到这里已经简单的实现了统一JSON格式了,但是发现了一个问题了,想要返回统一的JSON格式需要返回Result<Object>才可以,明明返回Object可以了,为什么要重复劳动,有没有解决方法,下面开始优化代码

    统一返回JSON格式进阶-全局处理(@RestControllerAdvice)

    使用@ResponseBody注解会把返回Object序列化成JSON字符串,在序列化前把Object赋值给Result<Object>就可以了,可以参考org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdviceorg.springframework.web.bind.annotation.ResponseBody

    @ResponseBody继承类

    @ResponseBody注解入手了就创建一个注解类继承@ResponseBody@ResponseResultBody 可以标记在类和方法上这样就可以跟自由的进行使用了
    1. @Retention(RetentionPolicy.RUNTIME)
    2. @Target({ElementType.TYPE, ElementType.METHOD})
    3. @Documented
    4. @ResponseBody
    5. public @interface ResponseResultBody {
    6. }

    ResponseBodyAdvice继承类

    1. @RestControllerAdvice
    2. public class ResponseResultBodyAdvice implements ResponseBodyAdvice<Object> {
    3. private static final Class<? extends Annotation> ANNOTATION_TYPE = ResponseResultBody.class;
    4. /**
    5. * 判断类或者方法是否使用了 @ResponseResultBody
    6. */
    7. @Override
    8. public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
    9. return AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ANNOTATION_TYPE) || returnType.hasMethodAnnotation(ANNOTATION_TYPE);
    10. }
    11. /**
    12. * 当类或者方法使用了 @ResponseResultBody 就会调用这个方法
    13. */
    14. @Override
    15. public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    16. // 防止重复包裹的问题出现
    17. if (body instanceof Result) {
    18. return body;
    19. }
    20. return Result.success(body);
    21. }
    22. }

    RestControllerAdvice返回测试

    1. @RestController
    2. @RequestMapping("/helloResult")
    3. @ResponseResultBody
    4. public class HelloResultController {
    5. private static final HashMap<String, Object> INFO;
    6. static {
    7. INFO = new HashMap<String, Object>();
    8. INFO.put("name", "galaxy");
    9. INFO.put("age", "70");
    10. }
    11. @GetMapping("hello")
    12. public HashMap<String, Object> hello() {
    13. return INFO;
    14. }
    15. /** 测试重复包裹 */
    16. @GetMapping("result")
    17. public Result<Map<String, Object>> helloResult() {
    18. return Result.success(INFO);
    19. }
    20. @GetMapping("helloError")
    21. public HashMap<String, Object> helloError() throws Exception {
    22. throw new Exception("helloError");
    23. }
    24. @GetMapping("helloMyError")
    25. public HashMap<String, Object> helloMyError() throws Exception {
    26. throw new ResultException();
    27. }
    28. }
    直接返回Object就可以统一JSON格式了,就不用每个返回都返回Result<T>对象了,直接让SpringMVC进行统一的管理。

    统一返回JSON格式进阶-异常处理(@ExceptionHandler)

    1. @Configuration
    2. public class MyExceptionHandler implements HandlerExceptionResolver {
    3. public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
    4. Object handler, Exception ex) {
    5. PrintWriter out = getPrintWrite(response);
    6. if (ex instanceof XXXException) {
    7. out.write(JsonUtil.formatJson(ResultEnum.PAY_ERROR.getCode(), ex.getMessage()));
    8. } else {
    9. out.write(JsonUtil.formatJson(ResultEnum.FAIL.getCode(), "服务器异常"));
    10. }
    11. if (null != out) {
    12. out.close();
    13. }
    14. return mav;
    15. }
    16. private PrintWriter getPrintWrite(HttpServletResponse response) {
    17. PrintWriter out = null;
    18. try {
    19. response.setHeader("Content-type", "text/html;charset=UTF-8");
    20. response.setCharacterEncoding("UTF-8");
    21. out = response.getWriter();
    22. } catch (IOException e) {
    23. log.error("PrintWriter is exception", e);
    24. }
    25. return out;
    26. }
    27. }
    上面的代码看看还是没有问题的,异常处理@ResponseStatus(不推荐),@ResponseStatus用法如下,可用在Controller类和Controller方法上以及Exception类上但是这样的工作量还是挺大的。
    1. @RestController
    2. @RequestMapping("/error")
    3. @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR, reason = "Java的异常")
    4. public class HelloExceptionController {
    5. private static final HashMap<String, Object> INFO;
    6. static {
    7. INFO = new HashMap<String, Object>();
    8. INFO.put("name", "galaxy");
    9. INFO.put("age", "70");
    10. }
    11. @GetMapping()
    12. public HashMap<String, Object> helloError() throws Exception {
    13. throw new Exception("helloError");
    14. }
    15. @GetMapping("helloJavaError")
    16. @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR, reason = "Java的异常")
    17. public HashMap<String, Object> helloJavaError() throws Exception {
    18. throw new Exception("helloError");
    19. }
    20. @GetMapping("helloMyError")
    21. public HashMap<String, Object> helloMyError() throws Exception {
    22. throw new MyException();
    23. }
    24. }
    25. @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR, reason = "自己定义的异常")
    26. class MyException extends Exception {
    27. }

    全局异常处理@ExceptionHandler(推荐)

    ResponseResultBodyAdvice类进行改造一下,主要参考org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler#handleException()方法,可以参考一下
    1. @Slf4j
    2. @RestControllerAdvice
    3. public class ResponseResultBodyAdvice implements ResponseBodyAdvice<Object> {
    4. private static final Class<? extends Annotation> ANNOTATION_TYPE = ResponseResultBody.class;
    5. /** 判断类或者方法是否使用了 @ResponseResultBody */
    6. @Override
    7. public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
    8. return AnnotatedElementUtils.hasAnnotation(returnType.getContainingClass(), ANNOTATION_TYPE) || returnType.hasMethodAnnotation(ANNOTATION_TYPE);
    9. }
    10. /** 当类或者方法使用了 @ResponseResultBody 就会调用这个方法 */
    11. @Override
    12. public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
    13. if (body instanceof Result) {
    14. return body;
    15. }
    16. return Result.success(body);
    17. }
    18. /**
    19. * 提供对标准Spring MVC异常的处理
    20. *
    21. * @param ex the target exception
    22. * @param request the current request
    23. */
    24. @ExceptionHandler(Exception.class)
    25. public final ResponseEntity<Result<?>> exceptionHandler(Exception ex, WebRequest request) {
    26. log.error("ExceptionHandler: {}", ex.getMessage());
    27. HttpHeaders headers = new HttpHeaders();
    28. if (ex instanceof ResultException) {
    29. return this.handleResultException((ResultException) ex, headers, request);
    30. }
    31. // TODO: 2019/10/05 galaxy 这里可以自定义其他的异常拦截
    32. return this.handleException(ex, headers, request);
    33. }
    34. /** 对ResultException类返回返回结果的处理 */
    35. protected ResponseEntity<Result<?>> handleResultException(ResultException ex, HttpHeaders headers, WebRequest request) {
    36. Result<?> body = Result.failure(ex.getResultStatus());
    37. HttpStatus status = ex.getResultStatus().getHttpStatus();
    38. return this.handleExceptionInternal(ex, body, headers, status, request);
    39. }
    40. /** 异常类的统一处理 */
    41. protected ResponseEntity<Result<?>> handleException(Exception ex, HttpHeaders headers, WebRequest request) {
    42. Result<?> body = Result.failure();
    43. HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
    44. return this.handleExceptionInternal(ex, body, headers, status, request);
    45. }
    46. /**
    47. * org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler#handleExceptionInternal(java.lang.Exception, java.lang.Object, org.springframework.http.HttpHeaders, org.springframework.http.HttpStatus, org.springframework.web.context.request.WebRequest)
    48. * <p>
    49. * A single place to customize the response body of all exception types.
    50. * <p>The default implementation sets the {@link WebUtils#ERROR_EXCEPTION_ATTRIBUTE}
    51. * request attribute and creates a {@link ResponseEntity} from the given
    52. * body, headers, and status.
    53. */
    54. protected ResponseEntity<Result<?>> handleExceptionInternal(
    55. Exception ex, Result<?> body, HttpHeaders headers, HttpStatus status, WebRequest request) {
    56. if (HttpStatus.INTERNAL_SERVER_ERROR.equals(status)) {
    57. request.setAttribute(WebUtils.ERROR_EXCEPTION_ATTRIBUTE, ex, WebRequest.SCOPE_REQUEST);
    58. }
    59. return new ResponseEntity<>(body, headers, status);
    60. }
    61. }