[英]Unable to test Spring Boot Jersey Rest API using TestRestTemplate Jar Conflict
@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);
}
}
My application executes perfectly and i am able to test the api using Postman . 我的应用程序执行完美,我能够使用Postman测试api 。 But when i try the above method to execute the test case to test the API, it gives the following error.
但是,当我尝试上述方法执行测试用例以测试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]
I have tried to check the this error and the almost all of them suggest (like this link and link ,) that there are multiple version of a jars(http-client/http-core) in my application. 我尝试检查此错误,几乎所有错误都表明(如此链接和链接 ),我的应用程序中有多个jars版本(http-client / http-core)。 I have checked and i did not find multiple version of
SSLConnectionSocketFactory
in my application. 我已经检查过,但在我的应用程序中找不到多个版本的
SSLConnectionSocketFactory
。 If at all there are conflicting jars, how can i know which are conflicting. 如果根本有冲突的罐子,我怎么知道哪个在冲突。 I do not manually add the jar, its a maven project.
我没有手动添加jar,它是一个maven项目。 I am stuck with this problem for the last 1 week and I am now at my wits end.
在过去的1周中,我一直困扰于这个问题,现在我机智了。
Please find below my pom.xml 请在下面找到我的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>
Any Help would be appreciated. 任何帮助,将不胜感激。
Quick answer 快速回答
Quick check of your pom.xml
shows that particular dependency org.owasp.esapi:esapi
pulls old 3.1
version of httpclient in. Try adding exclusion section to this dependency as described below. 快速检查
pom.xml
显示特定的依赖项org.owasp.esapi:esapi
将旧的3.1
版本的httpclient拉入其中。请尝试按如下所述向此依赖项添加排除部分。 If this does not help, work through dependency tree accordingly 如果这样做没有帮助,请相应地遍历依赖树
Full answer 完整答案
Run the following command under your project root folder 在您的项目根文件夹下运行以下命令
mvn dependency:tree -Dincludes=commons-httpclient
That will print your project's maven dependency tree with all pulled transitive dependencies with artifact id commons-httpclient
, for example: 这将打印项目的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
Than you could determine what is the source of wrong version of httpclient
coming in (for our example let it be org.owasp.esapi:esapi
) and then you can exclude that transitive dependency implicitly by amending your pom.xml: 比您可以确定
org.owasp.esapi:esapi
的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>
Note, that you may require to filter dependency tree using wildcards like 请注意,您可能需要使用通配符(例如
mvn dependency:tree -Dincludes=*http*
or even work through plain tree output calling mvn dependency:tree
without params 甚至通过调用
mvn dependency:tree
纯树输出来工作
And finally you can end up with explicit adding required dependency to your pom.xml having wrong versions found and excluded as decried above: 最后,您最终可以在pom.xml中显式添加所需的依赖项,从而发现并排除了错误的版本(如上所述):
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
Update 更新
As problem is not resolved by above. 由于上述问题无法解决。 Handy code piece to determine all the files being loaded from classpath:
方便的代码段,用于确定从类路径加载的所有文件:
ClassLoader cl = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)cl).getURLs();
for(URL url: urls){
System.out.println(url.getFile());
}
Can you put it into your contextLoads()
right before restTemplate
call and check/share the console output then? 您可以在
restTemplate
调用之前将其放到contextLoads()
然后检查/共享控制台输出吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.