【问题标题】:Unable to test Spring Boot Jersey Rest API using TestRestTemplate Jar Conflict无法使用 TestRestTemplate Jar Conflict 测试 Spring Boot Jersey Rest API
【发布时间】:2018-02-09 08:06:51
【问题描述】:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@Import({ JerseyConfig.class })
public class BenchApplicationTest {

@Autowired
private TestRestTemplate restTemplate;

@Test
public void contextLoads() {

    ResponseEntity<String> entity = this.restTemplate.getForEntity("/bench/healthcheck", String.class);
    assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);

  }

}

我的应用程序运行完美,我可以使用 Postman 测试 api。但是当我尝试上述方法执行测试用例来测试API时,它给出了以下错误。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testRestTemplate': Initialization of bean failed; nested exception is java.lang.NoSuchFieldError: INSTANCE
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:562) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.4.RELEASE.jar:4.3.4.RELEASE]
.....
Caused by: java.lang.NoSuchFieldError: INSTANCE
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.<clinit>(SSLConnectionSocketFactory.java:144) ~[httpclient-4.5.2.jar:4.5.2]
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:962) ~[httpclient-4.5.2.jar:1.2.2]

我已尝试检查此错误,并且几乎所有人都建议(例如 linklink,)我的应用程序中有多个版本的 jars(http-client/http-core) .我检查过,在我的应用程序中没有找到多个版本的SSLConnectionSocketFactory。如果有冲突的罐子,我怎么知道哪些是冲突的。我没有手动添加 jar,它是一个 maven 项目。在过去的 1 周里,我一直被这个问题困扰,现在我束手无策。

请在我的 pom.xml 下面找到

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.2.RELEASE</version>
</parent>
<dependencies>
    <!-- Spring dependencies -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.18.1</version>
    </dependency>
    <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>antlr-complete</artifactId>
        <version>3.5.2</version>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.3.2</version>
    </dependency>
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
    </dependency>
    <dependency>
        <groupId>commons-validator</groupId>
        <artifactId>commons-validator</artifactId>
        <version>1.4.1</version>
    </dependency>

    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.19.3</version>
    </dependency>
    <dependency>
        <groupId>net.sf.json-lib</groupId>
        <artifactId>json-lib</artifactId>
        <version>2.4</version>
    </dependency>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.7.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.antlr</groupId>
        <artifactId>antlr-complete</artifactId>
        <version>3.5.2</version>
    </dependency>
    <dependency>
        <groupId>com.rabbitmq</groupId>
        <artifactId>amqp-client</artifactId>
        <version>2.8.6</version>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
        <version>1.9.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>11.2.0.4</version>
    </dependency>
    <dependency>
        <groupId>org.owasp.esapi</groupId>
        <artifactId>esapi</artifactId>
        <version>2.1.0</version>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
    </dependency>
    <dependency>
        <groupId>org.freemarker</groupId>
        <artifactId>freemarker</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-jcs-jcache</artifactId>
        <version>2.0-beta-1</version>
    </dependency>
    <dependency>
        <groupId>org.reflections</groupId>
        <artifactId>reflections</artifactId>
        <version>0.9.9</version>
    </dependency>
    <dependency>
        <groupId>com.codahale.metrics</groupId>
        <artifactId>metrics-healthchecks</artifactId>
        <version>3.0.1</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
    </dependency>
</dependencies>

任何帮助将不胜感激。

【问题讨论】:

    标签: java rest maven spring-boot


    【解决方案1】:

    快速解答

    快速检查您的pom.xml 显示特定依赖项org.owasp.esapi:esapi 将旧的3.1 版本的httpclient 拉入。尝试如下所述将排除部分添加到此依赖项中。如果这没有帮助,请相应地处理依赖关系树

    完整答案

    在你的项目根文件夹下运行以下命令

    mvn dependency:tree -Dincludes=commons-httpclient
    

    这将打印您项目的 maven 依赖树,其中包含所有拉取的传递依赖项,其工件 ID 为 commons-httpclient,例如:

    [INFO] your:project:jar:1.0-SNAPSHOT
    [INFO] \- org.owasp.esapi:esapi:jar:2.1.0:compile
    [INFO]    \- org.owasp.antisamy:antisamy:jar:1.4.3:compile
    [INFO]       \- commons-httpclient:commons-httpclient:jar:3.1:compile
    

    您可以确定错误版本的httpclient 的来源是什么(对于我们的示例,让它成为org.owasp.esapi:esapi),然后您可以通过修改您的 pom.xml 来隐式排除该传递依赖:

            <dependency>
                <groupId>org.owasp.esapi</groupId>
                <artifactId>esapi</artifactId>
                <version>2.1.0</version>
                <!-- change starts here -->
                <exclusions>
                    <exclusion>
                        <groupId>commons-httpclient</groupId>
                        <artifactId>commons-httpclient</artifactId>
                    </exclusion>
                </exclusions>
                <!-- change ends here -->
            </dependency>
    

    请注意,您可能需要使用通配符过滤依赖树,例如

    mvn dependency:tree -Dincludes=*http*
    

    甚至通过普通树输出调用 mvn dependency:tree 而不使用参数

    最后你可以明确地向你的 pom.xml 添加所需的依赖项,发现错误的版本并将其排除在外:

    <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.2</version>
    </dependency>
    

    更新

    由于上述问题没有解决。方便的代码片段来确定从类路径加载的所有文件:

    ClassLoader cl = ClassLoader.getSystemClassLoader();
    
    URL[] urls = ((URLClassLoader)cl).getURLs();
    
    for(URL url: urls){
        System.out.println(url.getFile());
    }
    

    您可以在restTemplate 调用之前将其放入您的contextLoads() 并检查/共享控制台输出吗?

    【讨论】:

    • 我听从了你的建议......我删除了 httpclient 的其他传递关系,并按照你的建议明确定义了 httpclient......但问题仍然存在......同样的错误
    • 可能存在由于某种原因未正确下载依赖项的情况,这就是 maven 可能无法正确获取排除项的原因。请尝试使用mvn dependency:purge-local-repository 后跟mvn clean buld 重新下载jar 来清理本地存储库(注意:它将完全删除maven 本地存储库!)
    • 顺便问一下,能否请您仔细检查一下您的测试类路径中没有 IDE-add 依赖项? IE。一些通过 IDE 功能而不是通过 maven 导入项目的 JAR?某种全球图书馆或类似的东西
    • 我确定..我已经删除了其他传递 jars...在依赖层次结构中,只有一个 jar - httpclient:4.5.2 在 httpclient 过滤器之后。我也尝试过清理并构建...相同的错误检查过滤为“http”的依赖树的屏幕截图link
    • 我添加了“更新”部分,其中描述了如何将所有测试类路径 jar 打印到控制台,以便查看事实上的 httpclient jar。你能试试并通过pastebin分享控制台输出吗?
    猜你喜欢
    • 2017-01-05
    • 2020-06-22
    • 2019-01-14
    • 2023-03-25
    • 2017-11-02
    • 2020-08-02
    • 2017-04-23
    • 2014-08-12
    • 2016-11-06
    相关资源
    最近更新 更多