我使用HttpClient创建一个util类,用于通过Web项目中的http协议获取数据。
相关依赖如下:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.3</version>
</dependency>
我的util类的方法在这个项目中的单元测试方法中运行良好。但是当我开始通过jetty运行项目时,调用util类的相同方法,就会有一个InvocationTargetException来执行这个语句:
CloseableHttpClient httpClient = HttpClients.createDefault();
更多详情:
loader constraint violation: loader (instance of org/mortbay/jetty/webapp/WebAppClassLoader) previously initiated loading for a different type with name "javax/net/ssl/SSLSocketFactory"
java.lang.LinkageError: loader constraint violation: loader (instance of org/mortbay/jetty/webapp/WebAppClassLoader) previously initiated loading for a different type with name "javax/net/ssl/SSLSocketFactory"
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:792)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:366)
at org.mortbay.jetty.webapp.WebAppClassLoader.loadClass(WebAppClassLoader.java:337)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.(SSLConnectionSocketFactory.java:294)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.(SSLConnectionSocketFactory.java:261)
at org.apache.http.impl.client.HttpClientBuilder.build(HttpClientBuilder.java:955)
at org.apache.http.impl.client.HttpClients.createDefault(HttpClients.java:58)
at com.qunar.mobile.touch2.utils.http.Touch2HttpClient.doGet(Touch2HttpClient.java:36)
at com.qunar.mobile.touch2.utils.http.Touch2HttpClient.doGet(Touch2HttpClient.java:75)
at com.qunar.mobile.touch2.utils.http.Touch2HttpClient.doGet(Touch2HttpClient.java:83)
at com.qunar.mobile.touch2.service.hotel.activity.PizzaHutBreakfastService.queryCoupon(PizzaHutBreakfastService.java:21)
at com.qunar.mobile.touch2.controller.hotel.activity.PizzaHutBreakfast.execute(PizzaHutBreakfast.java:42)
at com.qunar.mobile.touch2.core.DispatcherServlet.doGet(DispatcherServlet.java:77)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093)
at com.qunar.mobile.touchwap.filter.SecurityFilter.doFilter(SecurityFilter.java:36)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
at qunar.management.HandlerContainer.handle(HandlerContainer.java:43)
at qunar.ServletWatcher.doFilter(ServletWatcher.java:90)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:726)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:206)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:514)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
使用jetty的Maven构建配置如下:
<build>
<plugins>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
<version>6.1.10</version>
<configuration>
<webAppConfig>
<contextPath>/</contextPath>
</webAppConfig>
<scanIntervalSeconds>10</scanIntervalSeconds>
<stopKey>foo</stopKey>
<stopPort>9999</stopPort>
</configuration>
<executions>
<execution>
<id>start-jetty</id>
<phase>pre-integration-test</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<scanIntervalSeconds>0</scanIntervalSeconds>
<daemon>true</daemon>
</configuration>
</execution>
<execution>
<id>stop-jetty</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
我使用的是JDK 1.7
似乎classloader发生了错误。
如何让我的util方法在这个Web项目中运行良好?
-------------------更新-----------------
我只是尝试将HttpClient的版本更改为4.3,然后现在一切正常。在jdk,jetty和HttpClient的版本中似乎存在一种神秘的效果。
虽然这个util方法现在可以工作,但我仍然想知道为什么抛出这个异常,如果我还想使用HttpClient 4.5.1,我该怎么做。
答案 0 :(得分:0)
Jetty 6.1.10是EOL(生命终结)。
截至今天,自那以来已有181个稳定的Jetty发布。
当项目迁移到Eclipse基础,并且Jetty 7.0被启动时,针对修复WebAppClassloader的Jetty 7.0.x(beta / milestone)代码库引入了此修复程序。 (javax.*
类是从Server类加载器强制加载的,不允许它们进入你有的加载器约束场景。
升级您的Jetty版本 - eclipse.org/jetty