使用Maven,Jersey,Tomcat,@ ApplicationPath,no web.xml调试IncompatibleClassChangeError

时间:2014-09-16 20:09:52

标签: java maven tomcat jersey jax-rs

如果我尝试加载任何资源网址,我会收到以下异常:

javax.servlet.ServletException: Servlet.init() for servlet com.example.project.Application threw exception
    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:316)
    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)

根本原因

java.lang.IncompatibleClassChangeError: org/glassfish/jersey/server/internal/scanning/AnnotationAcceptingListener$AnnotatedClassVisitor
    org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener.<init>(AnnotationAcceptingListener.java:143)
    org.glassfish.jersey.server.internal.scanning.AnnotationAcceptingListener.newJaxrsResourceAndProviderListener(AnnotationAcceptingListener.java:115)
    org.glassfish.jersey.server.ResourceConfig.scanClasses(ResourceConfig.java:884)
    org.glassfish.jersey.server.ResourceConfig._getClasses(ResourceConfig.java:849)
    org.glassfish.jersey.server.ResourceConfig.getClasses(ResourceConfig.java:755)
    com.sun.jersey.server.impl.application.DeferredResourceConfig$ApplicationHolder.<init>(DeferredResourceConfig.java:92)
    com.sun.jersey.server.impl.application.DeferredResourceConfig$ApplicationHolder.<init>(DeferredResourceConfig.java:79)
    com.sun.jersey.server.impl.application.DeferredResourceConfig.getApplication(DeferredResourceConfig.java:76)
    com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1164)
    com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:180)
    com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:799)
    com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:795)
    com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795)
    com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790)
    com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:491)
    com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:321)
    com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605)
    com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:376)
    com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:559)
    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:316)
    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制作了我的WAR文件。我<dependencies>中的pom.xml看起来像这样:

 <dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-grizzly2-http</artifactId>
    </dependency>
     <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>
    <!-- Viewable and @Template -->
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-mvc-mustache</artifactId>
        <version>2.12</version>
    </dependency>
    <!-- JUNIT -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
        <scope>test</scope>
    </dependency>
    <!-- hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.6.Final</version>
    </dependency>
    <!-- for JPA, use hibernate-entitymanager instead of hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.6.Final</version>
    </dependency>
    <!-- Hibernate uses slf4j for logging, for our purposes here use the simple backend -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.6.1</version>
    </dependency>
    <!-- Hibernate gives you a choice of bytecode providers between cglib and javassist -->
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.0.GA</version>
    </dependency>
    <!-- The tutorials use the H2 in-memory database -->
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.2.140</version>
    </dependency>
    <!-- needed for web.xml-less deploy -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-servlet</artifactId>
        <version>1.18.1</version>
    </dependency>
</dependencies>

如何调试此追溯?

1 个答案:

答案 0 :(得分:0)

对于依赖项,您希望添加与其他版本的东西兼容的servlet容器实现。也许:

<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>${jersey.version}</version>
</dependency>

卸下:

<!-- needed for web.xml-less deploy -->
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-servlet</artifactId>
    <version>1.18.1</version>
</dependency>

清除target目录并重新运行mvn package以生成新的war文件。