当前位置:   article > 正文

Springboot 全局异常处理 + 统一接口返回_spring responsehandler

spring responsehandler

1. R类返回

=>common\ResponseEnum(R枚举)

  1. public enum ResponseEnum {
  2. SUCCESS(200, "正确返回"),
  3. PAGE_NOT_FOUND(404, "找不到页面"),
  4. CODE_LOGIC_ERROR(500, "程序出错");
  5. private Integer status;
  6. private String message;
  7. private ResponseEnum(Integer status, String message){
  8. this.status = status;
  9. this.message = message;
  10. }
  11. public Integer getStatus(){
  12. return status;
  13. }
  14. public String getMessage(){
  15. return message;
  16. }
  17. }

=>common\R(R类)

  1. import lombok.AllArgsConstructor;
  2. import lombok.Data;
  3. import lombok.NoArgsConstructor;
  4. @Data
  5. @NoArgsConstructor
  6. @AllArgsConstructor
  7. public class R {
  8. private Integer status;
  9. private String message;
  10. private Object Data;
  11. public static R success(Object data, String message){
  12. R r = new R();
  13. r.setData(data);
  14. r.setMessage(message==null?ResponseEnum.SUCCESS.getMessage():message);
  15. r.setStatus(ResponseEnum.SUCCESS.getStatus());
  16. return r;
  17. }
  18. public static R success(Object data){
  19. return success(data, null);
  20. }
  21. public static R fail(Integer status, String message){
  22. R r = new R();
  23. r.setStatus(status);
  24. r.setMessage(message);
  25. r.setData(null);
  26. return r;
  27. }
  28. public static R fail(ResponseEnum responseEnum){
  29. R r = new R();
  30. r.setStatus(responseEnum.getStatus());
  31. r.setMessage(responseEnum.getMessage());
  32. r.setData(null);
  33. return r;
  34. }
  35. }

2. Error返回(会用R类包装返回的Error)

=>exception\Error(Error枚举)

  1. package com.wanghai.exception;
  2. public enum ErrorEnum {
  3. SUCCESS(true, 200, "正常结束"),
  4. PAGE_NOT_FOUND(false, 404, "页面找不到"),
  5. LOGIC_ERROR(false, 500, "代码逻辑错误"),
  6. BUSSINESS_ERROR(false, 20001, "业务出错"),
  7. ORDER_ERROR(false, 20002, "订单出错");
  8. private Integer status;
  9. private String message;
  10. private boolean resultFlag;
  11. private ErrorEnum(boolean resultFlag, Integer status, String message){
  12. this.resultFlag = resultFlag;
  13. this.status = status;
  14. this.message = message;
  15. }
  16. public Integer getStatus() {
  17. return status;
  18. }
  19. public String getMessage() {
  20. return message;
  21. }
  22. public boolean isResultFlag() {
  23. return resultFlag;
  24. }
  25. }

=>exception\ErrorHandler(Error处理器)

  1. package com.wanghai.exception;
  2. import lombok.Data;
  3. @Data
  4. public class ErrorHandler {
  5. private Integer status;
  6. private String massage;
  7. private String ExceptionClass;
  8. public static ErrorHandler fail(ErrorEnum errorEnum, Throwable throwable){
  9. ErrorHandler handler = new ErrorHandler();
  10. handler.setStatus(errorEnum.getStatus());
  11. handler.setMassage(errorEnum.getMessage());
  12. handler.setExceptionClass(throwable.getClass().getName());
  13. return handler;
  14. }
  15. public static ErrorHandler fail(ErrorEnum errorEnum, Throwable throwable, String message){
  16. ErrorHandler handler = fail(errorEnum, throwable);
  17. handler.setMassage(message);
  18. return handler;
  19. }
  20. // 针对自定义异常的 Handler
  21. public static ErrorHandler fail(ErrorEnum errorEnum, String message, Class classname){
  22. ErrorHandler handler = new ErrorHandler();
  23. handler.setStatus(errorEnum.getStatus());
  24. handler.setMassage(message==null?errorEnum.getMessage():message);
  25. handler.setExceptionClass(classname.toString());
  26. return handler;
  27. }
  28. }

=>exception\BussinessError & exception\OrderError(自定义异常)

  1. package com.wanghai.exception;
  2. import lombok.Data;
  3. @Data
  4. public class BussinessError extends RuntimeException{
  5. private Integer status;
  6. private String message;
  7. public BussinessError(Integer status, String message){
  8. this.status = status;
  9. this.message = message;
  10. }
  11. public BussinessError(ErrorEnum errorEnum){
  12. this.status = errorEnum.getStatus();
  13. this.message = errorEnum.getMessage();
  14. }
  15. }
  1. package com.wanghai.exception;
  2. import lombok.Data;
  3. @Data
  4. public class OrderError extends RuntimeException{
  5. private Integer status;
  6. private String message;
  7. public OrderError(Integer status, String message){
  8. this.status = status;
  9. this.message = message;
  10. }
  11. public OrderError(ErrorEnum errorEnum){
  12. this.status = errorEnum.getStatus();
  13. this.message = errorEnum.getMessage();
  14. }
  15. }

3. 全局异常处理 & 统一接口返回

=>config\GlobalExceptionHandler(全局异常处理)

  1. import com.wanghai.exception.BussinessError;
  2. import com.wanghai.exception.ErrorEnum;
  3. import com.wanghai.exception.OrderError;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.web.bind.annotation.ExceptionHandler;
  6. import org.springframework.web.bind.annotation.ResponseStatus;
  7. import org.springframework.web.bind.annotation.RestControllerAdvice;
  8. import javax.servlet.http.HttpServletRequest;
  9. import com.wanghai.exception.ErrorHandler;
  10. @RestControllerAdvice
  11. @Slf4j
  12. public class GlobalExceptionHandler {
  13. @ExceptionHandler(Throwable.class)
  14. public ErrorHandler makeException(Throwable throwable, HttpServletRequest request){
  15. ErrorHandler handler = ErrorHandler.fail(ErrorEnum.LOGIC_ERROR, throwable);
  16. log.error("错误发生的位置为:" + request.getRequestURL());
  17. System.out.println(handler);
  18. return handler;
  19. }
  20. @ExceptionHandler(OrderError.class)
  21. public ErrorHandler makeOrderException(HttpServletRequest request){
  22. ErrorHandler handler = ErrorHandler.fail(ErrorEnum.ORDER_ERROR, "订单出错了-c", OrderError.class);
  23. log.error("错误发生的位置为:" + request.getRequestURL());
  24. System.out.println(handler);
  25. return handler;
  26. }
  27. @ExceptionHandler(BussinessError.class)
  28. public ErrorHandler makeBussinessException(HttpServletRequest request){
  29. ErrorHandler handler = ErrorHandler.fail(ErrorEnum.ORDER_ERROR, "业务出错了-c", OrderError.class);
  30. log.error("错误发生的位置为:" + request.getRequestURL());
  31. System.out.println(handler);
  32. return handler;
  33. }
  34. }

=>config\ResultResponseHandler(统一接口返回)

  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import com.wanghai.common.R;
  3. import com.wanghai.exception.ErrorHandler;
  4. import lombok.SneakyThrows;
  5. import lombok.extern.slf4j.Slf4j;
  6. import org.springframework.core.MethodParameter;
  7. import org.springframework.http.MediaType;
  8. import org.springframework.http.server.ServerHttpRequest;
  9. import org.springframework.http.server.ServerHttpResponse;
  10. import org.springframework.web.bind.annotation.ControllerAdvice;
  11. import org.springframework.web.bind.annotation.RestControllerAdvice;
  12. import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
  13. @RestControllerAdvice
  14. @Slf4j
  15. public class ResultResponseHandler implements ResponseBodyAdvice<Object> {
  16. @Override
  17. public boolean supports(MethodParameter returnType, Class converterType) {
  18. return true;
  19. }
  20. @SneakyThrows
  21. @Override
  22. public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
  23. Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
  24. if(body instanceof ErrorHandler){
  25. ErrorHandler handler = (ErrorHandler) body;
  26. return R.fail(handler.getStatus(), handler.getMassage());
  27. } else if (body instanceof String){
  28. ObjectMapper objectMapper = new ObjectMapper();
  29. R r = R.success(body);
  30. return objectMapper.writeValueAsString(r);
  31. } else if(body instanceof R){
  32. return body;
  33. }
  34. return R.success(body);
  35. }
  36. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/184007?site
推荐阅读
相关标签
  

闽ICP备14008679号