use of org.springframework.web.bind.annotation.ExceptionHandler in project spring_boot by hryou0922.
the class BindExceptionHanlder method handleBindException.
@ExceptionHandler(BindException.class)
public Result handleBindException(BindException ex) {
// ex.getFieldError():随机返回一个对象属性的异常信息。如果要一次性返回所有对象属性异常信息,则调用ex.getAllErrors()
FieldError fieldError = ex.getFieldError();
StringBuilder sb = new StringBuilder();
sb.append(fieldError.getField()).append("=[").append(fieldError.getRejectedValue()).append("]").append(fieldError.getDefaultMessage());
// 生成返回结果
Result errorResult = new Result();
errorResult.setCode(400);
errorResult.setMessage(sb.toString());
return errorResult;
}
use of org.springframework.web.bind.annotation.ExceptionHandler in project spring_boot by hryou0922.
the class GlobalExceptionHandler method handleEmployeeNotFoundException.
@ExceptionHandler(EmployeeExJsonException.class)
@ResponseBody
public ExceptionJSONInfo handleEmployeeNotFoundException(HttpServletRequest request, Exception ex) {
ExceptionJSONInfo response = new ExceptionJSONInfo();
response.setUrl(request.getRequestURL().toString());
response.setMessage(ex.getMessage());
return response;
}
use of org.springframework.web.bind.annotation.ExceptionHandler in project tutorials by eugenp.
the class BaseController method exception.
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler({ SignatureException.class, MalformedJwtException.class, JwtException.class })
public JwtResponse exception(Exception e) {
JwtResponse response = new JwtResponse();
response.setStatus(JwtResponse.Status.ERROR);
response.setMessage(e.getMessage());
response.setExceptionType(e.getClass().getName());
return response;
}
use of org.springframework.web.bind.annotation.ExceptionHandler in project tutorials by eugenp.
the class RestResponseEntityExceptionHandler method handleAccessDeniedException.
@ExceptionHandler({ RepositoryConstraintViolationException.class })
public ResponseEntity<Object> handleAccessDeniedException(Exception ex, WebRequest request) {
RepositoryConstraintViolationException nevEx = (RepositoryConstraintViolationException) ex;
String errors = nevEx.getErrors().getAllErrors().stream().map(ObjectError::toString).collect(Collectors.joining("\n"));
return new ResponseEntity<>(errors, new HttpHeaders(), HttpStatus.NOT_ACCEPTABLE);
}
use of org.springframework.web.bind.annotation.ExceptionHandler in project tutorials by eugenp.
the class FileUploadExceptionAdvice method handleMaxSizeException.
@ExceptionHandler(MaxUploadSizeExceededException.class)
public ModelAndView handleMaxSizeException(MaxUploadSizeExceededException exc, HttpServletRequest request, HttpServletResponse response) {
ModelAndView modelAndView = new ModelAndView("file");
modelAndView.getModel().put("message", "File too large!");
return modelAndView;
}
Aggregations