# check **Repository Path**: zhu_c/check ## Basic Information - **Project Name**: check - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-08-24 - **Last Updated**: 2021-08-24 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 自定义验证信息源 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.support.ReloadableResourceBundleMessageSource; import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; import org.springframework.validation.beanvalidation.MethodValidationPostProcessor; import javax.validation.Validator; import java.util.Properties; /** * 配置 Hibernate 参数校验 * @author ludangxin * @date 2021/8/5 */ @Configuration public class ValidatorConfig { @Bean public MethodValidationPostProcessor methodValidationPostProcessor(Validator validator) { MethodValidationPostProcessor postProcessor = new MethodValidationPostProcessor(); postProcessor.setValidator(validator); return postProcessor; } ` /** * 实体类字段校验国际化引入 */ @Bean public Validator validator() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); // 设置messages资源信息 ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource(); // 多个用逗号分割 messageSource.setBasenames("classpath:/messages/validation/messages"); // 设置字符集编码 messageSource.setDefaultEncoding("UTF-8"); validator.setValidationMessageSource(messageSource); // 设置验证相关参数 Properties properties = new Properties(); // 快速失败,只要有错马上返回 properties.setProperty("hibernate.validator.fail_fast", "true"); validator.setValidationProperties(properties); return validator; } }` `├───resources └── messages └── validation └── messages.properties` `# messages.properties name.not.empty=用户名不能为空 email.not.valid=${validatedValue}是邮箱地址? email.not.empty=邮箱不能为空 phone.not.valid=${validatedValue}是手机号? phone.not.empty=手机号不能为空 password.size.valid=密码长度必须在{min}-{max}之间 id.not.empty=主键不能为空` ` import com.ldx.valid.annotation.Phone; import lombok.Data; import org.hibernate.validator.constraints.Range; import javax.validation.constraints.*; import java.io.Serializable; /** * 用户信息管理 * @author ludangxin * @date 2021/8/5 */ @Data public class SysUser implements Serializable { private static final long serialVersionUID = 1L; /** * 主键 */ @NotNull(message = "{id.not.empty}", groups = {ValidationInterface.update.class}) private Long id; /** * 用户名 */ @NotEmpty(message = "{name.not.empty}", groups = { ValidationInterface.update.class, ValidationInterface.add.class}) private String username; /** * 密码 */ @Size(min = 6, max = 16, message = "{password.size.valid}", groups = { ValidationInterface.update.class, ValidationInterface.add.class}) private String password = "123456"; /** * 邮箱地址 */ @Email(message = "{email.not.valid}", groups = { ValidationInterface.update.class, ValidationInterface.add.class, ValidationInterface.select.class}) @NotEmpty(message = "{email.not.empty}", groups = ValidationInterface.add.class) private String email; /** * 电话 */ @Pattern(message = "{phone.not.valid}", regexp = "^1[3456789]\\d{9}$", groups = {ValidationInterface.add.class}) @NotEmpty(message = "{phone.not.empty}", groups = {ValidationInterface.add.class}) private String phone; }` 注解 说明 @Null 限制只能为null @NotNull 限制必须不为null @AssertFalse 限制必须为false @AssertTrue 限制必须为true @DecimalMax(value) 限制必须为一个不大于指定值的数字 @DecimalMin(value) 限制必须为一个不小于指定值的数字 @Digits(integer,fraction) 限制必须为一个小数,且整数部分的位数不能超过integer,小数部分的位数不能超过fraction @Future 限制必须是一个将来的日期 @Max(value) 限制必须为一个不大于指定值的数字 @Min(value) 限制必须为一个不小于指定值的数字 @Past 限制必须是一个过去的日期 @Pattern(value) 限制必须符合指定的正则表达式 @Size(max,min) 限制字符长度必须在min到max之间 @Past 验证注解的元素值(日期类型)比当前时间早 @NotEmpty 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0) @NotBlank 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank @Email 验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式