• java中的全局异常捕获


    首先写一个封装实体的类

    1. package com.citicsc.framework.common.constructure.dto;
    2. import lombok.*;
    3. import lombok.experimental.Accessors;
    4. import java.io.Serializable;
    5. import java.util.Date;
    6. @Getter
    7. @Setter
    8. @ToString
    9. @NoArgsConstructor
    10. @Accessors(chain = true)
    11. @EqualsAndHashCode
    12. public class BaseResponseDTO implements Serializable {
    13. /**
    14. * requestId from request
    15. */
    16. private String requestId;
    17. /**
    18. * response DateTime
    19. */
    20. private Date responseDateTime = new Date();
    21. /**
    22. * response Status
    23. */
    24. private ResponseStatusEnum responseStatus;
    25. /**
    26. * response error code
    27. */
    28. private String responseErrorCode;
    29. /**
    30. * response error message
    31. */
    32. private String responseErrorMsg;
    33. /**
    34. * result
    35. */
    36. private T results;
    37. public static BaseResponseDTO buildFailureResp(String errCode, String errMsg, String requestId) {
    38. BaseResponseDTO resp = new BaseResponseDTO();
    39. resp.setResponseDateTime(new Date());
    40. resp.setResponseStatus(ResponseStatusEnum.FAIL);
    41. resp.setResponseErrorCode(errCode);
    42. resp.setResponseErrorMsg(errMsg);
    43. resp.setRequestId(requestId);
    44. return resp;
    45. }
    46. }

    然后自定义几个异常

    自定义个参数异常

    1. package com.citicsc.galaxy.finance.infrastructure.exceptions;
    2. public class ParamsErrorException extends RuntimeException {
    3. private static final long serialVersionUID = -5975068082716831797L;
    4. public ParamsErrorException(Exception e) {
    5. super(e);
    6. }
    7. public ParamsErrorException(String errorMessage) {
    8. super(errorMessage);
    9. }
    10. }

    在定义一个全局自己抛出的异常,在使用的时候就会自己new就行了,实例:

    1. if (dbBillData == null){
    2. throw new BizException("该账单不存在");
    3. }
    1. package com.citicsc.galaxy.finance.infrastructure.exceptions;
    2. public class BizException extends RuntimeException{
    3. /**
    4. *
    5. */
    6. private static final long serialVersionUID = 6131414147309131533L;
    7. public BizException(String message) {
    8. super(message);
    9. }
    10. }

    最后定义一个异常的捕获类,在类上面要加上@ControllerAdvice注解

    1. package com.citicsc.galaxy.finance.response;
    2. import com.citicsc.galaxy.finance.infrastructure.exceptions.BizException;
    3. import org.springframework.web.bind.MethodArgumentNotValidException;
    4. import org.springframework.web.bind.annotation.ControllerAdvice;
    5. import org.springframework.web.bind.annotation.ExceptionHandler;
    6. import org.springframework.web.bind.annotation.ResponseBody;
    7. import com.citicsc.framework.common.constructure.dto.BaseResponseDTO;
    8. import com.citicsc.galaxy.finance.infrastructure.exceptions.ParamsErrorException;
    9. import com.citicsc.galaxy.finance.infrastructure.response.FRExpCodeEnum;
    10. import com.citicsc.galaxy.finance.infrastructure.utils.ResponseUtils;
    11. import lombok.extern.slf4j.Slf4j;
    12. @Slf4j
    13. @ControllerAdvice
    14. public class GlobalExceptionHandler {
    15. @ExceptionHandler(Exception.class)
    16. @ResponseBody
    17. public BaseResponseDTO handleException(Exception e){
    18. log.error("### GlobalExceptionHandler.handleException", e);
    19. return ResponseUtils.fail(FRExpCodeEnum.SYS_ERROR.buildFullExpCode(), FRExpCodeEnum.SYS_ERROR.getErrorMessage());
    20. }
    21. @ExceptionHandler(ParamsErrorException.class)
    22. @ResponseBody
    23. public BaseResponseDTO handleParamsErrorException(ParamsErrorException e){
    24. log.error("### GlobalExceptionHandler.handleParamsErrorException", e);
    25. return ResponseUtils.fail(FRExpCodeEnum.PARAMS_ERROR.buildFullExpCode(), e.getMessage());
    26. }
    27. @ExceptionHandler(MethodArgumentNotValidException.class)
    28. @ResponseBody
    29. public BaseResponseDTO handleParamsErrorException(MethodArgumentNotValidException e) {
    30. log.error("### GlobalExceptionHandler.handleParamsErrorException", e);
    31. return ResponseUtils.fail(FRExpCodeEnum.PARAMS_ERROR.buildFullExpCode(),
    32. e.getBindingResult().getFieldError().getDefaultMessage());
    33. }
    34. @ExceptionHandler(BizException.class)
    35. @ResponseBody
    36. public BaseResponseDTO handleBizException(BizException e) {
    37. log.error("### GlobalExceptionHandler.handleBizException", e);
    38. return ResponseUtils.fail(FRExpCodeEnum.BUSINESS_ERROR.buildFullExpCode(),e.getMessage());
    39. }
    40. @ExceptionHandler(IllegalStateException.class)
    41. @ResponseBody
    42. public BaseResponseDTO handleIllegalStateException(IllegalStateException e) {
    43. log.error("### GlobalExceptionHandler.IllegalStateException", e);
    44. return ResponseUtils.fail(FRExpCodeEnum.BUSINESS_ERROR.buildFullExpCode(),e.getMessage());
    45. }
    46. }

  • 相关阅读:
    vm虚拟机硬盘扩充(linux)
    Ansys Zemax | 大功率激光系统的STOP分析2:如何进行光机械设计准备
    Linux学习之:进程概念
    mybatis初体验(细节满满)
    如何设计实时聊天系统的架构
    职责链模式(Chain of Responsibility Pattern)
    linux-进程管理
    chatGPT如何在Java中使用
    使用软引用实现缓存机制
    数据挖掘与分析课程笔记(Chapter 2)
  • 原文地址:https://blog.csdn.net/weixin_52774180/article/details/132871918