码迷,mamicode.com
首页 > 编程语言 > 详细

springboot全局异常处理

时间:2018-04-06 12:27:41      阅读:506      评论:0      收藏:0      [点我收藏+]

标签:lis   unknown   object   advice   entity   throw   efault   errors   ring   

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

}

springboot全局异常处理

标签:lis   unknown   object   advice   entity   throw   efault   errors   ring   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!