# validation-demo **Repository Path**: w17731138318/validation-demo ## Basic Information - **Project Name**: validation-demo - **Description**: 对象验证 validation-demo - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2023-02-13 - **Last Updated**: 2024-04-09 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # validation验证 ## 常用注解 | @AssertFalse | Boolean,boolean | 验证注解的元素值是false | | ------------------------------------ | ------------------------------------------------------------------------------ | ----------------------------------------------------------------------------- | | @AssertTrue | Boolean,boolean | 验证注解的元素值是true | | @NotNull | 任意类型 | 验证注解的元素值不是null | | @Null | 任意类型 | 验证注解的元素值是null | | @Min(value=值) | BigDecimal,BigInteger, byte,short, int, long,等任何Number或CharSequence(存储的是数字)子类型 | 验证注解的元素值大于等于@Min指定的value值 | | @Max(value=值) | 和@Min要求一样 | 验证注解的元素值小于等于@Max指定的value值 | | @DecimalMin(value=值) | 和@Min要求一样 | 验证注解的元素值大于等于@ DecimalMin指定的value值 | | @DecimalMax(value=值) | 和@Min要求一样 | 验证注解的元素值小于等于@ DecimalMax指定的value值 | | @Digits(integer=整数位数, fraction=小数位数) | 和@Min要求一样 | 验证注解的元素值的整数位数和小数位数上限 | | @Size(min=下限, max=上限) | 字符串、Collection、Map、数组等 | 验证注解的元素值的在min和max(包含)指定区间之内,如字符长度、集合大小 | | @Past | java.util.Date,java.util.Calendar;Joda Time类库的日期类型 | 验证注解的元素值(日期类型)比当前时间早 | | @Future | 与@Past要求一样 | 验证注解的元素值(日期类型)比当前时间晚 | | @NotBlank | CharSequence子类型 | 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的首位空格 | | @Length(min=下限, max=上限) | CharSequence子类型 | 验证注解的元素值长度在min和max区间内 | | @NotEmpty | CharSequence子类型、Collection、Map、数组 | 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0) | | @Range(min=最小值, max=最大值) | BigDecimal,BigInteger,CharSequence, byte, short, int, long等原子类型和包装类型 | 验证注解的元素值在最小值和最大值之间 | | @Email(regexp=正则表达式,flag=标志的模式) | CharSequence子类型(如String) | 验证注解的元素值是Email,也可以通过regexp和flag指定自定义的email格式 | | @Pattern(regexp=正则表达式,flag=标志的模式) | String,任何CharSequence的子类型 | 验证注解的元素值与指定的正则表达式匹配 | | @Valid | 任何非原子类型 | 指定递归验证关联的对象;如用户对象中有个地址对象属性,如果想在验证用户对象时一起验证地址对象的话,在地址对象上加@Valid注解即可级联验证
| ## 其他 @URL (protocol=,host,port) ## 自定义验证 定义注解 ``` @Target({METHOD,FIELD}) @Retention(RUNTIME) @Constraint(validatedBy = GenderValidator.class) @Documented public @interface Gender { String message() default "性别为男或者女"; Class[] groups() default {}; Class[] payload() default {}; } ``` GenderValidator ``` public class GenderValidator implements ConstraintValidator { private static final String MALE = "男"; private static final String FEMALE = "女"; @Override public void initialize(Gender constraintAnnotation) { } @Override public boolean isValid(String value, ConstraintValidatorContext context) { return MALE.equals(value) || FEMALE.equals(value); } } ``` @ControllerAdvice 拦截返回错误 ``` @ControllerAdvice public class ConstraintViolationHandler { /** * 捕获方法参数校验异常 */ @ExceptionHandler(ConstraintViolationException.class) @ResponseBody public ResponseEntity> constraintViolationExceptionHandler(ConstraintViolationException e){ Set> message = e.getConstraintViolations(); HashMap map = new HashMap<>(); message.forEach(msg ->{ String path = msg.getPropertyPath().toString(); String field = path.substring(path.indexOf(".")+1); map.put(field,msg.getMessageTemplate()); }); return ResponseEntity.ok(map); } /** * 捕获实体参数校验异常 */ @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseBody public ResponseEntity> resolveMethodArgumentNotValidException(MethodArgumentNotValidException e){ List allErrors = e.getBindingResult().getAllErrors(); HashMap map = new HashMap<>(); allErrors.forEach(error -> { FieldError fieldError = (FieldError) error; map.put(fieldError.getField(),fieldError.getDefaultMessage()); }); return ResponseEntity.ok(map); } } ``` ## 分组验证 AddGroup ``` public interface AddGroup { } ``` 使用 验证对象属性添加 ``` @NotBlank(message = "请输入名称", groups = AddGroup.class) ``` 请求接口添加 ``` @Validated({AddGroup.class}) ``` ## 国际化 resources 下创建 i18n文件 validation_common.properties validation_common_en_US.properties validation_common_en_CN.properties ... ``` name.require=用户名不能为空 ``` 初始化bean ``` @Configuration public class ValidationConfig { @Bean public Validator getValidator() { ResourceBundleMessageSource bundleMessageSource = new ResourceBundleMessageSource(); bundleMessageSource.setDefaultEncoding("UTF-8"); bundleMessageSource.setBasenames("i18n/validation", "i18n/validation_common"); LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.setValidationMessageSource(bundleMessageSource); return validator; } } ``` 使用时前端 http请求 header 添加 ``` Accept-Language: zh-CN zh-CN 简体中文 zh-TW 繁体中文 en-US 英文 ```