• 微服务项目中自定义异常类


    1. /**
    2. * @author w
    3. * @desc 自定义异常类 捕获自动映射实体类参数校验异常
    4. */
    5. @ControllerAdvice
    6. @ResponseBody
    7. public class MyExceptionHandle {
    8. private static Logger logger = LoggerFactory.getLogger(MyExceptionHandle.class);
    9. @ExceptionHandler(MethodArgumentNotValidException.class)
    10. public BaseResponse handleBindException(MethodArgumentNotValidException ex) {
    11. FieldError fieldError = ex.getBindingResult().getFieldError();
    12. logger.error("MethodArgumentNotValidException {}:{}({})", ResultEnum.RESPONSE_MSG_PARAM_INVALID.getMessage(), fieldError.getDefaultMessage(),fieldError.getField());
    13. BaseResponse response = new BaseResponse();
    14. response.setCode(ResultEnum.RESPONSE_MSG_PARAM_INVALID.getCode());
    15. response.setMessage(ResultEnum.RESPONSE_MSG_PARAM_INVALID.getMessage() + fieldError.getDefaultMessage());
    16. logger.info(JSON.toJSONString(response));
    17. return response;
    18. }
    19. @ExceptionHandler(BindException.class)
    20. public BaseResponse handleBindException(BindException ex) {
    21. //校验 除了 requestbody 注解方式的参数校验 对应的 bindingresult 为 BeanPropertyBindingResult
    22. FieldError fieldError = ex.getBindingResult().getFieldError();
    23. logger.error("BindException {}:{}({})", ResultEnum.RESPONSE_MSG_PARAM_INVALID.getMessage(), fieldError.getDefaultMessage(), fieldError.getField());
    24. String errMsg = fieldError.getDefaultMessage();
    25. if(StringUtils.isBlank(errMsg)) {
    26. errMsg = fieldError.getField() + "不能为空";
    27. }
    28. BaseResponse response = new BaseResponse();
    29. response.setCode(ResultEnum.RESPONSE_MSG_PARAM_INVALID.getCode());
    30. response.setMessage(ResultEnum.RESPONSE_MSG_PARAM_INVALID.getMessage() + errMsg);
    31. logger.info(JSON.toJSONString(response));
    32. return response;
    33. }
    34. @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    35. public BaseResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException ex) {
    36. //拦截http请求方法不允许 ,如:post方法使用get方式
    37. String error = ex.getMessage();
    38. logger.error("HttpRequestMethodNotSupportedException \n{}\n请求方式:{}\n允许方式:{}", error, ex.getMethod(), ex.getSupportedMethods());
    39. BaseResponse response = new BaseResponse();
    40. response.setCode(ResultEnum.RESPONSE_MSG_METHOD_NOT_SUPPORT.getCode());
    41. response.setMessage(ResultEnum.RESPONSE_MSG_METHOD_NOT_SUPPORT.getMessage() + error + " by " + ex.getSupportedMethods()[0]);
    42. logger.info(JSON.toJSONString(response));
    43. return response;
    44. }
    45. @ExceptionHandler(RuntimeException.class)
    46. public BaseResponse handleException(RuntimeException ex) {
    47. ex.printStackTrace();
    48. //拦截异常
    49. logger.error("RuntimeException \n{}\n{}\n{}\n{}", ex.getLocalizedMessage(), ex.getCause(), ex.getStackTrace(), ex.getMessage());
    50. BaseResponse response = new BaseResponse();
    51. response.setCode(ResultEnum.RESPONSE_MSG_ERROR.getCode());
    52. response.setMessage(ResultEnum.RESPONSE_MSG_ERROR.getMessage() + ex.getMessage());
    53. logger.info(JSON.toJSONString(response));
    54. return response;
    55. }
    56. }

    效果

     

  • 相关阅读:
    火车头采集怎么使用GPT等AI原创文章
    2024Node.js零基础教程(小白友好型),nodejs新手到高手,(九)NodeJS入门——http模块
    随笔荟萃 | sincerity
    搭建nuxt3项目(框架构建)
    将 Jupyter Notebook 用于 JavaScript (使用 Node.js 或 Deno 环境)
    C专家编程 第6章 运行的诗章:运行时数据结构 6.13 只适用于高级学员阅读的材料
    Qt 之自定义控件(开关按钮)
    链表专项练习(四)
    Grafana 系列文章(十五):Exemplars
    5. kafka单机版本
  • 原文地址:https://blog.csdn.net/wen_3370/article/details/127755766