Java 如何使用spring boots将具有字段的对象保存到自定义验证器?

Java 如何使用spring boots将具有字段的对象保存到自定义验证器?,java,spring-boot,validation,Java,Spring Boot,Validation,有人能帮我解释一下为什么我不能在spring boots中用自定义验证器保存具有字段的对象吗 情景: 首先,我必须通过自定义验证器验证该字段(该验证器工作正常),然后将实体保存到数据库中(这会中断)。 我正在IntellijIDE上使用SpringBoots框架。代码在github上 我有客户实体 @实体 公共类客户{ @身份证 @GeneratedValue(策略=GenerationType.AUTO) 私人长id; @ContactInfo//自定义验证程序 @NotNull 私人字符串联

有人能帮我解释一下为什么我不能在spring boots中用自定义验证器保存具有字段的对象吗

情景: 首先,我必须通过自定义验证器验证该字段(该验证器工作正常),然后将实体保存到数据库中(这会中断)。 我正在IntellijIDE上使用SpringBoots框架。代码在github上

我有客户实体

@实体
公共类客户{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人长id;
@ContactInfo//自定义验证程序
@NotNull
私人字符串联系人信息;
//标准构造函数、getter、setter
}
我有ContactInfoExpression实体

@实体
公共类ContactInfoExpression{
@身份证
@列(name=“表达式类型”)
私有字符串类型;
私有字符串模式;
//标准构造函数、getter、setter
}
我有
ContactInfoExpressionRepository
CustomerRepository
它扩展了
crudepository

我在application.properties文件中使用具有以下配置的H2内存中数据库。
contactInfoType
属性可以设置为值email、phone或website之一

spring.h2.console.enabled=true
spring.h2.console.path=/h2
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.show-sql=true

contactInfoType=email
#contactInfoType=phone
#contactInfoType=website
自定义验证器

@组件
公共类ContactInfoValidator实现ConstraintValidator{
私有静态最终记录器LOG=LogManager.getLogger(ContactInfoValidator.class);
@值(“${contactInfoType}”)
私有字符串表达式类型;
私有字符串模式;
@自动连线
私有ContactInfoExpressionRepository ContactInfoExpressionRepository;
@凌驾
公共无效初始化(ContactInfo ContactInfo){
if(StringUtils.isEmptyOrWhitespace(expressionType)){
日志。错误(“缺少联系人信息类型!”);
}否则{
模式=contactInfoExpressionRepository.findById(expressionType)
.map(ContactInfoExpression::getPattern).get();
}
}
@凌驾
公共布尔值有效(字符串值、ConstraintValidatorContext上下文){
如果(!StringUtils.isEmptyOrWhitespace(模式)){
返回模式。匹配(模式、值);
}
LOG.error(“联系人信息模式丢失!”);
返回false;
}
}
自定义约束注释

@Constraint(validatedBy={ContactInfoValidator.class})
@目标({方法,字段,注释类型,构造函数,参数})
@保留(RetentionPolicy.RUNTIME)
public@interface ContactInfo{
字符串消息()默认为“无效值”;
类[]组()默认值{};

Class问题的解决方案是在
应用程序.properties
文件中添加以下内容

properties-spring.jpa.properties.javax.persistence.validation.mode:none

参考: 如何避免Spring Boot应用程序中的双重验证,Sanjay Patel,2018年5月15日

有任何错误/问题吗?我编辑了一篇有错误的文章。我正在使用IntelliJ IDE进行开发。完整的代码在github上。验证是一个配置,而不是一个组件?没有必要添加
@configuration
@component
注释,因为springboot在开始时启动自定义验证器。pro的解决方案问题是在application.properties文件中添加
spring.jpa.properties.javax.persistence.validation.mode:none
。此站点的代码太多。请参阅。
java.lang.IllegalStateException: Failed to execute CommandLineRunner

Caused by: org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction

Caused by: javax.persistence.RollbackException: Error while committing the transaction

Caused by: javax.validation.ValidationException: HV000032: Unable to initialize com.example.customvalidation.ContactInfoValidator.

Caused by: java.lang.NullPointerException: null
    at com.example.customvalidation.ContactInfoValidator.initialize(ContactInfoValidator.java:41) ~[classes/:na]
    at com.example.customvalidation.ContactInfoValidator.initialize(ContactInfoValidator.java:18) ~[classes/:na]