1.在实体类上添加注解

2.在需要判断的方法上添加 @Valid

3.配置异常拦截
@Slf4j
@ControllerAdvice
public class MyExceptionHandle {
@Autowired
private MessageSourceHandler messageSourceHandler;
@ExceptionHandler(value = Exception.class)
@ResponseBody
public BaseResponse handle(Exception e) {
e.printStackTrace();
if(e instanceof SmartBusinessException) {
SmartBusinessException exception = (SmartBusinessException)e;
String message = messageSourceHandler.getMessage(exception.getMsg(),exception.getArgs());
return BaseResponse.error(exception.getCode(),message);
}else if (e instanceof MethodArgumentNotValidException){
SmartBusinessException exception = new SmartBusinessException();
exception.setMsg(Objects.requireNonNull(((MethodArgumentNotValidException) e).getBindingResult().getFieldError()).getDefaultMessage());
exception.setCode("402");
return BaseResponse.error(exception.getCode(),exception.getMsg());
}else{
log.error("系统异常",e);
return BaseResponse.error("99999",messageSourceHandler.getMessage("COMMON_SERVER_ERROR"));
}
}