简体   繁体   中英

When using HttpClient in a web project running by jetty I comeacross an InvocationTargetException

I use HttpClient to make an util class for getting data through http protocol in a web project.

Relative dependicies are like below:

<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>

My util class's method works well in a unit test method in this project. But when I start running the project by jetty, invoke the util class's same method, there's an InvocationTargetException for the execution of this statement:

CloseableHttpClient httpClient = HttpClients.createDefault(); 

more details :

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)

Maven build configuration for using jetty is here:

<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>

I use JDK 1.7

It seems that something wrong happened with classloader.

What should I do to make my util method work well in this web project?

------------------- Update -----------------

I just tried to change HttpClient's version to 4.3, then everything is ok now.There seems to exist a mysterious effect among the versions of jdk, jetty and HttpClient.

Although this util method can work now, I still want to know why this exception is thrown and if I still want to use HttpClient 4.5.1, what should I do.

Jetty 6.1.10 is EOL (End of Life).

As of today, there have been 181 releases of stable Jetty since.

When the project moved to the Eclipse foundation, and Jetty 7.0 was spun up, the fix for this was introduced to Jetty 7.0.x (beta/milestone) codebase fixing the WebAppClassloader. (the javax.* classes are force loaded from the Server classloader, not allowing them to enter this loader constraint scenario you have)

Upgrade your version of Jetty - eclipse.org/jetty

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM