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())));
}

}
原文地址:https://www.cnblogs.com/xifenglou/p/8727127.html