Jersey/Tomcat 应用程序在升级到 jersey 2.21 后没有为 ReflectionHelper.resolveField 抛出此类方法异常

Jersey/Tomcat app throws no such method execption for ReflectionHelper.resolveField after upgrade to jersey 2.21

我在 tomcat 中有一个非常简单的 Jersey 2 应用程序 运行。它是用 maven 构建的,maven 生成一个 war 文件。当我使用 Jersey 2.16 时,它运行良好。我的 pom 中有 "RELEASE" 用于依赖版本,今天我重建了 Jersey 2.21。转到2.21后出现此错误。我有 oracle jdk 8 和 tomcat 7.0.39

    Aug 26, 2015 12:16:26 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet  threw load() exception
java.lang.NoSuchMethodError: org.glassfish.hk2.utilities.reflection.ReflectionHelper.resolveField(Ljava/lang/Class;Ljava/lang/reflect/Field;)Ljava/lang/reflect/Type;
    at org.jvnet.hk2.internal.Utilities.getFieldInjectees(Utilities.java:1911)
    at org.jvnet.hk2.internal.ClazzCreator.initialize(ClazzCreator.java:160)
    at org.jvnet.hk2.internal.ClazzCreator.initialize(ClazzCreator.java:179)
    at org.jvnet.hk2.internal.SystemDescriptor.internalReify(SystemDescriptor.java:723)
    at org.jvnet.hk2.internal.SystemDescriptor.reify(SystemDescriptor.java:678)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.reifyDescriptor(ServiceLocatorImpl.java:463)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.reifyDescriptor(ServiceLocatorImpl.java:477)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.checkConfiguration(ServiceLocatorImpl.java:1620)
    at org.jvnet.hk2.internal.ServiceLocatorImpl.addConfiguration(ServiceLocatorImpl.java:2005)
    at org.jvnet.hk2.internal.DynamicConfigurationImpl.commit(DynamicConfigurationImpl.java:246)
    at org.glassfish.jersey.internal.inject.Injections.bind(Injections.java:156)
    at org.glassfish.jersey.internal.inject.Injections._createLocator(Injections.java:144)
    at org.glassfish.jersey.internal.inject.Injections.createLocator(Injections.java:123)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:328)
    at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:390)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:170)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:362)
    at javax.servlet.GenericServlet.init(GenericServlet.java:160)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5123)

这是我的 Maven 配置:

        ...
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.glassfish.jersey</groupId>
                <artifactId>jersey-bom</artifactId>
                <version>RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
...
       <dependency>
        <groupId>org.jasypt</groupId>
        <artifactId>jasypt</artifactId>
        <version>RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.jasypt</groupId>
        <artifactId>jasypt-spring3</artifactId>
        <version>RELEASE</version>
    </dependency>


    <dependency>
        <groupId>com.fasterxml.jackson.jaxrs</groupId>
        <artifactId>jackson-jaxrs-json-provider</artifactId>
        <version>RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-spring3</artifactId>
        <version>RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>RELEASE</version>
    </dependency>
    <dependency>
        <groupId>com.datastax.cassandra</groupId>
        <artifactId>cassandra-driver-core</artifactId>
        <version>${cassandra.driver.version}</version>
    </dependency>
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.1</version>
    </dependency>

我也遇到过这个问题。在我的例子中,它是由两个版本的 Jersey 库 - 2.19 和 2.21 - 一起使用引起的。

org.glassfish.jersey.containers:jersey-container-servlet:2.19
org.glassfish.jersey.ext:jersey-spring3:2.21

供日后搜索类似例外情况的人使用。

对我来说,我不得不从 jersey-container-servlet:

中排除旧的 2.4.0-b34 版本的 hk2-locator
compile (group: 'org.glassfish.jersey.containers', 
         name: 'jersey-container-servlet', 
         version: '2.23.1') {
              exclude group: 'org.glassfish.hk2', module: 'hk2-locator' 
}

因为传递 2.4.0-b34 被包括在内。使用 hk2-locator:2.4.0 明确解决了它。

可能,对于 Jersey/hk2/Jackson 中的所有 NoSuchMethodError,通过打印出 Maven/Gradle 依赖关系树来匹配 Jersey 与正确的依赖关系。