@ControllerAdvice
public class GlobalExceptionHandler {

@ExceptionHandler({Exception.class})
@ResponseBody
public Object exception(Exception e, HttpServletRequest request, HttpServletResponse response) {

//参数校验get
if(e instanceof ConstraintViolationException){
StringBuffer sb = new StringBuffer();
ConstraintViolationException exs = (ConstraintViolationException)e;
Set<ConstraintViolation<?>> violations = exs.getConstraintViolations();
for(ConstraintViolation<?> item:violations){
sb.append(item.getMessage()+"/");
}
return new BaseRespBackEnd(101,"请求参数错误",sb.toString());
}

//参数校验post
if(e instanceof MethodArgumentNotValidException){
MethodArgumentNotValidException exs = (MethodArgumentNotValidException)e;
BindingResult bindingResult = exs.getBindingResult();
StringBuffer sb = new StringBuffer();
for(FieldError fieldError :bindingResult.getFieldErrors()){
sb.append(fieldError.getDefaultMessage()+"/");
}
return new BaseRespBackEnd(101,"请求参数错误",sb.toString());
}
}
}

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-09-06
  • 2021-09-29
  • 2021-11-13
  • 2021-09-27
  • 2021-05-25
猜你喜欢
  • 2022-12-23
  • 2021-09-28
  • 2021-11-26
  • 2021-10-31
  • 2022-12-23
相关资源
相似解决方案