java-如何在Tomcat 8中使用Bean验证2.0(JSR 380)功能?
作者:互联网
Bean验证2.0(JSR 380)应该能够执行以下验证:
@Valid
public class MyBean{
private List<@NotNull String> members;
}
但它似乎只是忽略@NotNull批注.此外,@ NotBlank和@NotEmpty批注将被忽略.
我通过Maven在我的类路径中有Bean Validation 2.0 API和参考实现:
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.15.Final</version>
</dependency>
我将Java 8和Tomcat 8与Jersey一起使用.
我是否需要以某种方式配置Tomcat以使用2.0实现,而不是JDK 8中捆绑的默认实现(如在Jackson中启用Optional支持)?
解决方法:
问题的原因是,针对Jersey的Bean验证扩展隐式地带来了较旧版本的Hibernate验证器,而未使用我在pom.xml中显式请求的Hibernate验证器.
为了解决这个问题,我排除了Jersey隐式拉出的休眠验证器:
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-bean-validation</artifactId>
<version>2.27</version>
<exclusions>
<exclusion>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</exclusion>
</exclusions>
</dependency>
添加我的Bean验证版本
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.0.15.Final</version>
</dependency>
并添加了Java EL:
<dependency>
<groupId>org.glassfish</groupId>
<artifactId>javax.el</artifactId>
<version>3.0.1-b09</version>
</dependency>
<dependency>
<groupId>javax.el</groupId>
<artifactId>javax.el-api</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
标签:hibernate,tomcat,bean-validation,java 来源: https://codeday.me/bug/20191024/1922955.html