GlobalExceptionHandler.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package com.aijia.kmt.common;
  2. import com.aijia.core.base.ResponseEntity;
  3. import com.aijia.core.security.exception.CommonException;
  4. import com.aijia.core.web.ResponseConstant;
  5. import com.aijia.core.web.exception.RequestsException;
  6. import org.apache.commons.lang3.StringUtils;
  7. import org.slf4j.Logger;
  8. import org.slf4j.LoggerFactory;
  9. import org.springframework.http.HttpStatus;
  10. import org.springframework.http.converter.HttpMessageNotReadableException;
  11. import org.springframework.validation.FieldError;
  12. import org.springframework.web.HttpMediaTypeNotSupportedException;
  13. import org.springframework.web.HttpRequestMethodNotSupportedException;
  14. import org.springframework.web.bind.MethodArgumentNotValidException;
  15. import org.springframework.web.bind.annotation.ControllerAdvice;
  16. import org.springframework.web.bind.annotation.ExceptionHandler;
  17. import org.springframework.web.bind.annotation.ResponseBody;
  18. import org.springframework.web.bind.annotation.ResponseStatus;
  19. import org.springframework.web.util.ContentCachingRequestWrapper;
  20. import javax.servlet.http.HttpServletRequest;
  21. import java.nio.charset.Charset;
  22. import java.util.List;
  23. /**
  24. *
  25. * 类名称:ExceptionAdvice<br>
  26. * 类描述:异常处理<br>
  27. * @author yrf
  28. * @version v1.0
  29. */
  30. @ControllerAdvice
  31. @ResponseBody
  32. public class GlobalExceptionHandler {
  33. private final Logger logger = LoggerFactory.getLogger(this.getClass());
  34. /**
  35. * 400 - Bad Request
  36. */
  37. @ResponseStatus(HttpStatus.BAD_REQUEST)
  38. @ExceptionHandler(HttpMessageNotReadableException.class)
  39. public ResponseEntity handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
  40. logger.error("参数解析失败", e);
  41. return ResponseEntity.failure("无法解析JSON");
  42. }
  43. @ResponseStatus(HttpStatus.EXPECTATION_FAILED)
  44. @ExceptionHandler(RequestsException.class)
  45. public ResponseEntity handleRequestsException(RequestsException e) {
  46. logger.warn("请求太频繁,稍后再试", e);
  47. return ResponseEntity.failure("请求太频繁,稍后再试");
  48. }
  49. /**
  50. * 405 - Method Not Allowed
  51. */
  52. @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
  53. @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
  54. public ResponseEntity handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
  55. logger.error("不支持当前请求方法", e);
  56. return ResponseEntity.failure("不支持当前请求方法");
  57. }
  58. /**
  59. * 415 - Unsupported Media Type
  60. */
  61. @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
  62. @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
  63. public ResponseEntity handleHttpMediaTypeNotSupportedException(Exception e) {
  64. logger.error("不支持当前媒体类型", e);
  65. return ResponseEntity.failure("不支持的内容类型");
  66. }
  67. /**
  68. * 500 - Internal Server Error
  69. */
  70. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  71. @ExceptionHandler(Exception.class)
  72. public ResponseEntity handleException(HttpServletRequest request, Exception e) throws Exception{
  73. String bodyStr = null;
  74. if (request instanceof ContentCachingRequestWrapper) {
  75. ContentCachingRequestWrapper contentCachingRequestWrapper = (ContentCachingRequestWrapper) request;
  76. bodyStr = StringUtils.toEncodedString(contentCachingRequestWrapper.getContentAsByteArray(), Charset.forName(contentCachingRequestWrapper.getCharacterEncoding()));
  77. }
  78. logger.error(String.format("服务运行异常,uri = [%s] \n请求参数 body = %s", request.getRequestURI(), bodyStr), e);
  79. return ResponseEntity.failure();
  80. }
  81. /**
  82. * 500 - Internal Server Error
  83. * 用于给用户提示时抛出,如:操作失败的原因
  84. */
  85. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  86. @ExceptionHandler(CommonException.class)
  87. public ResponseEntity handleCommonException(CommonException e) {
  88. return ResponseEntity.failure(ResponseConstant.CODE_000, e.getMessage());
  89. }
  90. /**
  91. * 500 - Internal Server Error
  92. * 用于处理断言抛出的异常提示
  93. */
  94. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  95. @ExceptionHandler(IllegalArgumentException.class)
  96. public ResponseEntity handleIllegalArgumentExceptionException(IllegalArgumentException e) {
  97. return ResponseEntity.failure(e.getMessage());
  98. }
  99. /**
  100. * 500 - Internal Server Error
  101. * 参数校验失败时抛出,如:非空属性的值为空
  102. */
  103. @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
  104. @ExceptionHandler(MethodArgumentNotValidException.class)
  105. public ResponseEntity handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
  106. StringBuilder errorInfo = new StringBuilder();
  107. List<FieldError> fieldErrorList = e.getBindingResult().getFieldErrors();
  108. fieldErrorList.forEach(fieldError -> errorInfo.append(fieldError.getDefaultMessage()).append(","));
  109. errorInfo.deleteCharAt(errorInfo.length() - 1);
  110. return ResponseEntity.failure(ResponseConstant.CODE_000, errorInfo.toString());
  111. }
  112. }