Spring HtmlUnit依赖

时间:2014-10-12 09:10:44

标签: java spring maven htmlunit

我有一个带有HtmlUnit依赖项的Spring MVC应用程序。我在@Component类中使用HtmlUnit实用程序。问题是编译完成后( root cause java.lang.NoClassDefFoundError:org / apache / http / HttpEntity ),在浏览器启动时(索引页面),我看到的页面是:

> HTTP Status 500 - Servlet.init() for servlet mvc-dispatcher threw
> exception
type Exception report
message Servlet.init() for servlet mvc-dispatcher threw exception
description The server encountered an internal error that prevented it from fulfilling this request.
exception javax.servlet.ServletException: Servlet.init() for servlet mvc-dispatcher threw exception

root cause java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
root cause java.lang.ClassNotFoundException: org.apache.http.HttpEntity


org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
    java.lang.Class.getDeclaredConstructors0(Native Method)
    java.lang.Class.privateGetDeclaredConstructors(Class.java:2532)
    java.lang.Class.getDeclaredConstructors(Class.java:1901)
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.determineCandidateConstructors(AutowiredAnnotationBeanPostProcessor.java:230)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.determineConstructorsFromBeanPostProcessors(AbstractAutowireCapableBeanFactory.java:976)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:949)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)
org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)
    org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598)
org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661)
org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517)
    org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458)
    org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
    javax.servlet.GenericServlet.init(GenericServlet.java:158)
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501)
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

我使用maven包含了所有依赖项,这是我的 pom.xml

<dependency>
    <groupId>net.sourceforge.htmlunit</groupId>
    <artifactId>htmlunit</artifactId>
    <version>2.15</version>
</dependency>
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.1</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.3.5</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.3.5</version>
</dependency>

如果从类中删除@Component注释 - 应用程序在启动时不会失败,但在实际的控制器调用中,它使用类(内置HtmlUnit) - 同样的错误。如果我从控制器中删除使用HtmlUnit的类,一切都很好。 我也试过自己添加库,而不使用maven - 没有帮助以太。 关于它的主要谜团是,我的班级(使用HtmlUnit)在通常的控制台应用程序中完美地工作。所以我尝试了很多东西,但我仍然没有得到这个的原因问题,为什么它不适用于Spring ???也许HtmlUnit仅适用于一个线程应用程序?但为什么会有ClassNotFoundException?

1 个答案:

答案 0 :(得分:1)

你是如何运行这个应用程序的?您是否在tomcat服务器中将其作为war存档运行?如果是这样,你的war存档是否包含/ WEB-INF / lib文件夹中的htmlunit依赖项?运行应用程序的运行时上下文显然没有与控制台应用程序相同的类路径。

你的htmlunit依赖项应该在/ WEB-INF / lib下,或者在服务器类路径中(不建议使用后者)。