使用校验器对传入的参数进行校验时,如何抛出message中的异常提示语
@ApiModelProperty("售价")
@Max(value = 999999999,message = "价格最大值为999999999")
private Double price;
抛异常时发现抛出的异常类型是MethodArgumentNotValidException
对数据校验抛出的异常进行全局异常捕获
@ExceptionHandler(value = {MethodArgumentNotValidException.class})
@ResponseBody
public Result bindExceptionHandle(MethodArgumentNotValidException e) {
e.printStackTrace();
BindingResult bindingResult = e.getBindingResult();
StringBuilder errorMsg = new StringBuilder();
bindingResult.getAllErrors().forEach(x -> errorMsg.append(x.getDefaultMessage()).append("!"));
return Result.error(ResponseEnum.PARAM_ERROR.getResultCode(), errorMsg.toString());
}