GlobalExceptionHandler.java 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package com.fdkankan.common.exception;
  2. import com.fdkankan.common.model.Result;
  3. import com.fdkankan.common.model.ViewResult;
  4. import lombok.extern.slf4j.Slf4j;
  5. import org.springframework.context.support.DefaultMessageSourceResolvable;
  6. import org.springframework.http.HttpStatus;
  7. import org.springframework.validation.BindException;
  8. import org.springframework.web.bind.MethodArgumentNotValidException;
  9. import org.springframework.web.bind.MissingServletRequestParameterException;
  10. import org.springframework.web.bind.annotation.ExceptionHandler;
  11. import org.springframework.web.bind.annotation.ResponseStatus;
  12. import org.springframework.web.bind.annotation.RestControllerAdvice;
  13. import javax.servlet.http.HttpServletRequest;
  14. import java.util.stream.Collectors;
  15. /**
  16. * 全局异常捕获统一返回客户端
  17. * @author Admin
  18. */
  19. @Slf4j
  20. @RestControllerAdvice
  21. public class GlobalExceptionHandler {
  22. /**
  23. * 处理其他异常
  24. * @param e
  25. * @return
  26. */
  27. @ExceptionHandler(value = Exception.class)
  28. public ViewResult exceptionHandler(Exception e){
  29. log.error("程序错误原因:", e);
  30. return ViewResult.error("服务端异常");
  31. }
  32. /**
  33. * 处理其他异常
  34. * @param e
  35. * @return
  36. */
  37. @ExceptionHandler(value = MissingServletRequestParameterException.class)
  38. public ViewResult exceptionHandler(MissingServletRequestParameterException e){
  39. log.error("缺少必要参数:", e);
  40. return ViewResult.error(HttpStatus.BAD_REQUEST.value(), "缺少必要参数");
  41. }
  42. /**
  43. * 处理请求参数格式错误 @RequestBody上validate失败后抛出的异常是MethodArgumentNotValidException异常
  44. */
  45. @ExceptionHandler(MethodArgumentNotValidException.class)
  46. public ViewResult MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
  47. String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
  48. //下边ResultCodeEnum.PARAMS_BS_ERROR.getCode()就是你自己自定义的返回code码
  49. return ViewResult.error(HttpStatus.BAD_REQUEST.value(), message);
  50. }
  51. /**
  52. * 处理Get请求中 使用@Valid 验证路径中请求实体校验失败后抛出的异常
  53. */
  54. @ExceptionHandler(BindException.class)
  55. public ViewResult BindExceptionHandler(BindException e) {
  56. String message = e.getBindingResult().getAllErrors().stream().map(DefaultMessageSourceResolvable::getDefaultMessage).collect(Collectors.joining());
  57. return ViewResult.error(HttpStatus.BAD_REQUEST.value(), message);
  58. }
  59. /**
  60. * @link https://www.cnblogs.com/cjyboy/p/11465876.html
  61. * 处理请求参数格式错误 @RequestParam上validate失败后抛出的异常是ConstraintViolationException
  62. */
  63. // @ExceptionHandler(ConstraintViolationException.class)
  64. // public ViewResult ConstraintViolationExceptionHandler(ConstraintViolationException e) {
  65. // String message = e.getConstraintViolations().stream().map(ConstraintViolation::getMessage).collect(Collectors.joining());
  66. // return ViewResult.error(HttpStatus.BAD_REQUEST.value(), message);
  67. // }
  68. @ExceptionHandler(BaseRuntimeException.class)
  69. @ResponseStatus(HttpStatus.OK)
  70. public Result runtimeExceptionHandler(HttpServletRequest request, BaseRuntimeException e) {
  71. log.error("uri:{},exception:{}",request.getRequestURI(),e);
  72. return Result.failure(e.getCode() == null ? Result.CODE_FAILURE : e.getCode(), e.getMsg());
  73. }
  74. }