@NotNull@NotBlank@NotEmpty之间的区别
- @NotNull
适用于基本数据类型(Integer,Long,Double等等),当 @NotNull 注解被使用在 String 类型的数据上,则表示该数据不能为 Null(但是可以为 Empty) - @NotBlank
适用于 String 类型的数据上,加了@NotBlank 注解的参数不能为 Null 且 trim() 之后 size > 0 - @NotEmpty
适用于 String、Collection集合、Map、数组等等,加了@NotEmpty 注解的参数不能为 Null 或者 长度为 0
类型 | 区别 |
---|---|
@NotNull | 基本数据类型 |
@NotBlank | String |
@NotEmpty | 数组集合 |
异常抛出
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(value = HttpStatus.OK)
public ResponseWrapper<?> handleException(HttpServletRequest request, MethodArgumentNotValidException ex) {
ResponseWrapper<?> response = new ResponseWrapper<Object>(Constants.UNRECOGNIZED_ERROR_CODE, ex.getBindingResult().getFieldError().getDefaultMessage());
fillingException(response, ex);
return response;
}