| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package com.aijia.kmt.common;
- import com.aijia.core.base.ResponseEntity;
- import com.aijia.core.security.exception.CommonException;
- import com.aijia.core.web.ResponseConstant;
- import com.aijia.core.web.exception.RequestsException;
- import org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.converter.HttpMessageNotReadableException;
- import org.springframework.validation.FieldError;
- import org.springframework.web.HttpMediaTypeNotSupportedException;
- import org.springframework.web.HttpRequestMethodNotSupportedException;
- import org.springframework.web.bind.MethodArgumentNotValidException;
- import org.springframework.web.bind.annotation.ControllerAdvice;
- import org.springframework.web.bind.annotation.ExceptionHandler;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.bind.annotation.ResponseStatus;
- import org.springframework.web.util.ContentCachingRequestWrapper;
- import javax.servlet.http.HttpServletRequest;
- import java.nio.charset.Charset;
- import java.util.List;
- /**
- *
- * 类名称:ExceptionAdvice<br>
- * 类描述:异常处理<br>
- * @author yrf
- * @version v1.0
- */
- @ControllerAdvice
- @ResponseBody
- public class GlobalExceptionHandler {
- private final Logger logger = LoggerFactory.getLogger(this.getClass());
- /**
- * 400 - Bad Request
- */
- @ResponseStatus(HttpStatus.BAD_REQUEST)
- @ExceptionHandler(HttpMessageNotReadableException.class)
- public ResponseEntity handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
- logger.error("参数解析失败", e);
- return ResponseEntity.failure("无法解析JSON");
- }
- @ResponseStatus(HttpStatus.EXPECTATION_FAILED)
- @ExceptionHandler(RequestsException.class)
- public ResponseEntity handleRequestsException(RequestsException e) {
- logger.warn("请求太频繁,稍后再试", e);
- return ResponseEntity.failure("请求太频繁,稍后再试");
- }
-
- /**
- * 405 - Method Not Allowed
- */
- @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
- @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
- public ResponseEntity handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
- logger.error("不支持当前请求方法", e);
- return ResponseEntity.failure("不支持当前请求方法");
- }
- /**
- * 415 - Unsupported Media Type
- */
- @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
- @ExceptionHandler(HttpMediaTypeNotSupportedException.class)
- public ResponseEntity handleHttpMediaTypeNotSupportedException(Exception e) {
- logger.error("不支持当前媒体类型", e);
- return ResponseEntity.failure("不支持的内容类型");
- }
- /**
- * 500 - Internal Server Error
- */
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
- @ExceptionHandler(Exception.class)
- public ResponseEntity handleException(HttpServletRequest request, Exception e) throws Exception{
- String bodyStr = null;
- if (request instanceof ContentCachingRequestWrapper) {
- ContentCachingRequestWrapper contentCachingRequestWrapper = (ContentCachingRequestWrapper) request;
- bodyStr = StringUtils.toEncodedString(contentCachingRequestWrapper.getContentAsByteArray(), Charset.forName(contentCachingRequestWrapper.getCharacterEncoding()));
- }
- logger.error(String.format("服务运行异常,uri = [%s] \n请求参数 body = %s", request.getRequestURI(), bodyStr), e);
- return ResponseEntity.failure();
- }
- /**
- * 500 - Internal Server Error
- * 用于给用户提示时抛出,如:操作失败的原因
- */
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
- @ExceptionHandler(CommonException.class)
- public ResponseEntity handleCommonException(CommonException e) {
- return ResponseEntity.failure(ResponseConstant.CODE_000, e.getMessage());
- }
- /**
- * 500 - Internal Server Error
- * 用于处理断言抛出的异常提示
- */
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
- @ExceptionHandler(IllegalArgumentException.class)
- public ResponseEntity handleIllegalArgumentExceptionException(IllegalArgumentException e) {
- return ResponseEntity.failure(e.getMessage());
- }
- /**
- * 500 - Internal Server Error
- * 参数校验失败时抛出,如:非空属性的值为空
- */
- @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
- @ExceptionHandler(MethodArgumentNotValidException.class)
- public ResponseEntity handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
- StringBuilder errorInfo = new StringBuilder();
- List<FieldError> fieldErrorList = e.getBindingResult().getFieldErrors();
- fieldErrorList.forEach(fieldError -> errorInfo.append(fieldError.getDefaultMessage()).append(","));
- errorInfo.deleteCharAt(errorInfo.length() - 1);
- return ResponseEntity.failure(ResponseConstant.CODE_000, errorInfo.toString());
- }
- }
|