为什么方法级别验证在 Spring 中不起作用?
Why does method level validation not work in Spring?
学生class:
package com.solvegen.education.student.enrollment.core.model;
import com.sun.istack.internal.NotNull;
import java.util.Date;
public class Student {
private Long id;
@NotNull
private String userName;
@NotNull
private String firstName;
@NotNull
private String lastName;
@NotNull
private String password;
@NotNull
private String emailAddress;
@NotNull
private Date dateOfBirth;
public Date getDateOfBirth() {
return dateOfBirth;
}
public String getEmailAddress() {
return emailAddress;
}
public String getFirstName() {
return firstName;
}
public Long getId() {
return id;
}
public String getLastName() {
return lastName;
}
public String getPassword() {
return password;
}
public String getUserName() {
return userName;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setId(Long id) {
this.id = id;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
spring-config.xml
的内容
<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
</bean>
<bean id="validatorPostProcessor"
class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor">
<property name="validator" ref="validator"/>
</bean>
Spring 服务的定义:
@Service("studentService")
@Validated
public class StudentService {
@Autowired
private StudentMapper studentMapper;
@Transactional
public void insertStudent(@Valid Student student) {
studentMapper.insertStudent(student);
}
}
pom.xml
的内容
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.1.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
当我在方法声明中使用 @NotNull
而不是 @Valid
时,一切正常(当我传递 null 而不是 Student class 的实例时,验证框架抛出异常)。
我做错了什么?或者 String 中的方法级别验证不支持参数的 @Valid 注解?
问题出在 com.sun.istack.internal.NotNull。我从不正确的包中导入。必须是 javax.validation.constraints.NotNull
学生class:
package com.solvegen.education.student.enrollment.core.model;
import com.sun.istack.internal.NotNull;
import java.util.Date;
public class Student {
private Long id;
@NotNull
private String userName;
@NotNull
private String firstName;
@NotNull
private String lastName;
@NotNull
private String password;
@NotNull
private String emailAddress;
@NotNull
private Date dateOfBirth;
public Date getDateOfBirth() {
return dateOfBirth;
}
public String getEmailAddress() {
return emailAddress;
}
public String getFirstName() {
return firstName;
}
public Long getId() {
return id;
}
public String getLastName() {
return lastName;
}
public String getPassword() {
return password;
}
public String getUserName() {
return userName;
}
public void setDateOfBirth(Date dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public void setId(Long id) {
this.id = id;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public void setPassword(String password) {
this.password = password;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
spring-config.xml
的内容<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
</bean>
<bean id="validatorPostProcessor"
class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor">
<property name="validator" ref="validator"/>
</bean>
Spring 服务的定义:
@Service("studentService")
@Validated
public class StudentService {
@Autowired
private StudentMapper studentMapper;
@Transactional
public void insertStudent(@Valid Student student) {
studentMapper.insertStudent(student);
}
}
pom.xml
的内容<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.2.1.Final</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.2</version>
</dependency>
当我在方法声明中使用 @NotNull
而不是 @Valid
时,一切正常(当我传递 null 而不是 Student class 的实例时,验证框架抛出异常)。
我做错了什么?或者 String 中的方法级别验证不支持参数的 @Valid 注解?
问题出在 com.sun.istack.internal.NotNull。我从不正确的包中导入。必须是 javax.validation.constraints.NotNull