我试图验证这个bean:
public class NewUserCompanyForm implements Serializable {
private static final long serialVersionUID = 6414340627544822204L;
@Email(groups = ValidationGroup1.class)
@NotBlank(groups = ValidationGroup1.class)
@Length(min = 5)
private String userName;
@NotBlank(groups = ValidationGroup1.class)
private String password;
@NotBlank(groups = ValidationGroup1.class)
private String repeatedPassword;
@NotBlank(groups = ValidationGroup2.class)
private String companyName;
@Pattern(regexp = "\\d\\d-\\d\\d\\d", groups = ValidationGroup2.class)
private String postalCode;
@NotBlank(groups = ValidationGroup2.class)
@Pattern(regexp = "\\p{L}*", groups = ValidationGroup2.class)
private String city;
@Pattern(regexp = "[-A-Za-z0-9 ,.]*", groups = ValidationGroup2.class)
@NotBlank(groups = ValidationGroup2.class)
private String addressDetails;
不幸的是,我几乎所有的字段验证都获得了例外:
javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Email' validating type 'java.lang.String'. Check configuration for 'userName'
总是UnexpectedTypeException: HV000030
。我阅读了Hibernate和Spring验证的文档,一切似乎都没问题,例如@Email
注释可以与字符串一起使用。
编辑:添加了pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<thymeleaf.spring.version>3.0.7.RC1</thymeleaf.spring.version>
<thymeleaf.version>3.0.7.RELEASE</thymeleaf.version>
<hibernate.validation.version>6.0.2.Final</hibernate.validation.version>
<hibernate.version>5.2.11.Final</hibernate.version>
<hibernate.commons.version>5.0.1.Final</hibernate.commons.version>
<bean.validation.api.version>2.0.0.Final</bean.validation.api.version>
<mysql.connector.java.version>6.0.6</mysql.connector.java.version>
<spring.version>5.0.0.RELEASE</spring.version>
<spring.session.version>1.3.1.RELEASE</spring.session.version>
<spring.security.version>4.2.3.RELEASE</spring.security.version>
<spring.webflow.version>2.4.6.RELEASE</spring.webflow.version>
<spring.boot.test.version>2.0.0.M5</spring.boot.test.version>
<common.codecs>1.10</common.codecs>
</properties>
<groupId>pwr.groupproject.vouchers</groupId>
<artifactId>voucherApp</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>${spring.session.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring5 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
<version>${thymeleaf.spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.validator/hibernate-validator -->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validation.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.validation/validation-api -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>${bean.validation.api.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.connector.java.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>${thymeleaf.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.1-api -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.security.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.common/hibernate-commons-annotations -->
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>${hibernate.commons.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${common.codecs}</version>
</dependency>
<dependency>
<groupId>org.springframework.webflow</groupId>
<artifactId>spring-webflow</artifactId>
<version>${spring.webflow.version}</version>
</dependency>
</dependencies>
<repositories>
</repositories>
以下是用于测试验证的类:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {SpringWebFlowConfiguration.class, WebConfiguration.class})
public class NewUserCompanyFormValidationTest {
@Autowired
@Qualifier("localValidatorFactoryBean")
private LocalValidatorFactoryBean validator;
@Autowired
private MessageSource messageSource;
@Test
public void DependencyInjectionTest(){
Assert.assertNotNull(validator);
}
@Test
public void NewUserCompanyFormValidatingTest(){
NewUserCompanyForm form1=new NewUserCompanyForm();
form1.setPassword("");
form1.setUserName("");
form1.setRepeatedPassword("");
Set<ConstraintViolation<NewUserCompanyForm>> volations1=validator.validate(form1,NewUserCompanyForm.ValidationGroup1.class);
}
}
答案 0 :(得分:0)
基于对https://stackoverflow.com/a/17086477/1840078的解读,我猜测该问题是多余的,可能是冲突的bean验证注释。当存在@Email
注释时,@NotBlank
注释是多余的,@Length
注释也可能是多余的。有效的电子邮件地址不能为空,并且对于在其完全限定的主机名中具有顶级域的电子邮件主机,其长度必须至少为5。
答案 1 :(得分:0)
检查一下...
@Email注释没有@Constraint(第36行)
@Constraint(validatedBy = { })
在validation-api-2.0.1.Final中,因此最终结果为javax.servlet.ServletException: HV000030: No validator could be found for constraint 'javax.validation.constraints.Email'