使用springboot向类路径错误添加一个实现,例如Hibernate Validator

问题描述 投票:0回答:5
spring maven spring-boot configuration pom.xml
5个回答
6
投票

您缺少一些库。将以下 Spring Boot 依赖项添加到您的

pom.xml
来解决它。 Spring Boot 将找出在您的项目中使用的正确实现。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

另外,添加 Hibernate 依赖项:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- Hibernate -->
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-ehcache</artifactId>
  <exclusions>
    <exclusion>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
    </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.hibernate</groupId>
  <artifactId>hibernate-java8</artifactId>
</dependency>

5
投票

根据此链接,最新的 Spring Boot 版本应该已修复:https://github.com/spring-projects/spring-boot/pull/12669(允许在类路径中验证 api,而不需要实现)。对于较旧的启动版本,修复此异常的另一个选项是排除验证 API。使用 spring-boot-starter web 时的示例如下:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>        
        <exclusion>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

1
投票

我只需要添加:

compile      'org.springframework.boot:spring-boot-starter-validation'


1
投票

我使用的是 Spring Boot 2.4.4 版本。最初,我使用了下面的 spring 验证器依赖项,但它不起作用。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

但后来,我使用了下面的 hibernate 依赖项,它在我的代码中完美运行

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.0.2.Final</version>
</dependency>

0
投票

是的,@viniciusjssouza 的答案对我有用。我的Spring Boot版本是2.7。 即使你没有 Hibernte.Validator,只需将此依赖项添加到 Gradle 或类似 Maven

实现“org.springframework.boot:spring-boot-starter-validation”

© www.soinside.com 2019 - 2024. All rights reserved.