springboot全局异常处理
@Slf4j
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler({InvalidRequestException.class})
public ResponseEntity<?> handleInvalidRequest(HttpServletRequest request, InvalidRequestException e) {
log.warn("invalid request exception {}", e);
return ResponseEntity.status(HttpStatus.CONFLICT).body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(e.getErrorCode().isForm() ? HttpStatus.GONE : HttpStatus.CONFLICT, "logic error", e.getErrorCode() == null ? null : e.getErrorCode().getCode(),
e.getMessage() == null ? ResponseErrorCode.GENERAL_PARAMS_ERROR.getDescription() : e.getMessage(), request.getServletPath()), "**"));
}
@ExceptionHandler({FormulaErrorException.class})
public ResponseEntity<?> handleFormula(HttpServletRequest request, FormulaErrorException e) {
log.warn("invalid request exception {}", e);
return ResponseEntity.status(HttpStatus.CONFLICT).body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.GONE, "logic error", ResponseErrorCode.CONFIG_FORMULA_ERROR.getCode(),
e.getMessage() == null ? ResponseErrorCode.CONFIG_FORMULA_ERROR.getDescription() : e.getMessage(), request.getServletPath()), "**"));
}
@ExceptionHandler(value = {AccessDeniedException.class})
public ResponseEntity<?> handlerAccessDeniedException(HttpServletRequest request, HttpServletResponse response, AccessDeniedException ex) throws IOException {
/* if (Objects.equals(ex.getMessage(), "不允许访问")) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.UNAUTHORIZED,
"Forbidden", "no authority", request.getServletPath())));
} else {
return ResponseEntity.status(HttpStatus.FORBIDDEN)
.body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.FORBIDDEN,
"Forbidden", "Access Denied", request.getServletPath())));
}*/
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.UNAUTHORIZED,
"Forbidden", "no authority", request.getServletPath())));
}
@ExceptionHandler
public ResponseEntity<?> handleException(HttpServletRequest request, Exception e) {
log.warn("invalid request exception {}", e);
return ResponseEntity.status(HttpStatus.CONFLICT).body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.CONFLICT, "system error", ResponseErrorCode.GENERAL_UNKNOWN_AUTHORITY.getCode(),
e.getMessage() == null ? ResponseErrorCode.GENERAL_UNKNOWN_AUTHORITY.getDescription() : e.getMessage(), request.getServletPath()), "**"));
}
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors();
List<String> errors = new ArrayList<>(fieldErrors.size() + globalErrors.size());
String error;
for (FieldError fieldError : fieldErrors) {
error = fieldError.getField() + ", " + fieldError.getDefaultMessage();
errors.add(error);
}
for (ObjectError objectError : globalErrors) {
error = objectError.getObjectName() + ", " + objectError.getDefaultMessage();
errors.add(error);
}
return ResponseEntity.status(HttpStatus.CONFLICT)
.body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.GONE,
"invalidate parameter", ResponseErrorCode.GENERAL_PARAMS_ERROR.getCode(),
StringUtils.join(errors), ((ServletWebRequest) request).getRequest().getServletPath())));
}
}
------------------你若盛开 - 蝴蝶自来------------------
如果,您认为阅读这篇博客让您有些收获, 如果,您希望更容易地发现我的新博客,不妨关注一下。因为,我的写作热情也离不开您的肯定支持。
感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客。
因为有小孩,兼职卖书,路过的朋友有需要低价购买图书、点读笔、纸尿裤等资源的,可扫最上方二维码,质量有保证,价格很美丽,欢迎咨询! 欢迎关注微信公众号:18般武艺
如果,您认为阅读这篇博客让您有些收获, 如果,您希望更容易地发现我的新博客,不妨关注一下。因为,我的写作热情也离不开您的肯定支持。
感谢您的阅读,如果您对我的博客所讲述的内容有兴趣,请继续关注我的后续博客。
因为有小孩,兼职卖书,路过的朋友有需要低价购买图书、点读笔、纸尿裤等资源的,可扫最上方二维码,质量有保证,价格很美丽,欢迎咨询! 欢迎关注微信公众号:18般武艺