@NotEmpty:不能为null,并且长度必须大于0java
@NotBlank:只用在String上,表示传进来的值不能为null,并且调用trim()后,长度必须大于0
@NotNull:不能为null,但能够为empty
.net
若是存在嵌套的对象,在定义对象的上面添加@Valid 就能够了code
@Valid
使用方式以下,在参数接收的那里 加上 @Valid对象
还能够添加一个全局的异常处理,这样也能够避免写一堆的if判断数据是否为空,固然这也有必定的局限性,若是要想验证是数据其余的合法性,就须要单独处理,或者自定义注解也能够搞定。blog
@ControllerAdvice @RestController public class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseBody public ResultMsgModel handleValidException(MethodArgumentNotValidException e) { Log.logger.error("参数校验失败 " + e.getParameter().getMethod() + " " + e.getBindingResult().getAllErrors().get(0).getDefaultMessage()); return new ResultMsgModel(ResponseCode.RETURN_VERIFICATION_FAIL,e.getBindingResult().getAllErrors().get(0).getDefaultMessage()); } }