在 Java Jersey 应用程序中启动时出现 NoSuchMethodError

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28509370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:21:24  来源:igfitidea点击:

NoSuchMethodError on startup in Java Jersey app

javamavenjerseyjax-rsjersey-2.0

提问by SGr

I've been getting a very strange error when trying to start a Jersey app on Tomcat. The same code works on other computers. I tried reinstalling tomcat, all my maven dependencies, even Eclipse and Java itself, no luck. It seems like a bad Jersey version is being loaded, I think?

尝试在 Tomcat 上启动 Jersey 应用程序时,我遇到了一个非常奇怪的错误。相同的代码适用于其他计算机。我尝试重新安装 tomcat,我所有的 maven 依赖项,甚至 Eclipse 和 Java 本身,但没有运气。似乎正在加载一个糟糕的 Jersey 版本,我想?

Any pointers in the right direction will be appreciated.

任何指向正确方向的指针将不胜感激。

Here's the effective pom: http://pastebin.com/NacsWTjz

这是有效的 pom:http: //pastebin.com/NacsWTjz

And the actual pom: http://pastebin.com/H6sHe4ce

和实际的 pom:http: //pastebin.com/H6sHe4ce

2015-02-13 13:43:40,870 [localhost-startStop-1] ERROR org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/middleware-server] - StandardWrapper.Throwable
java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:304)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:285)
    at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:170)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:358)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4901)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5188)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1399)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

采纳答案by Paul Samsotha

Note:Please see above comments for further discussion and tips.

注意:有关进一步讨论和提示,请参阅以上评论。

This error usual means that you have a both a JAX-RS 1 and JAX-RS 2 jar on the classpath. Jersey 2 uses JAX-RS 2 (javax.ws.rs-api-2.0.1.jar), but if you have the jsr311-api.jaralso, which is JAX-RS 1, there is a javax.ws.rs.core.Applicationin each jar. But the jsr311-apiApplicationdoesn't have the method getProperties()(hence NoSuchMethodError).

此错误通常意味着您在类路径上同时具有 JAX-RS 1 和 JAX-RS 2 jar。Jersey 2 使用 JAX-RS 2 ( javax.ws.rs-api-2.0.1.jar),但如果您jsr311-api.jar也有 JAX-RS 1,则javax.ws.rs.core.Application每个 jar 中都有一个。但是jsr311-apiApplication没有方法getProperties()(因此NoSuchMethodError)。

I've come to the conclusion that all you need to do is add the above exclusion to the swagger dependency. The Hymanson 2.0 provider (which depends on JAX-RS 1) seems to be overridden by a 2.4.1 provider (which uses the new version). So we don't need to add it ourselves. When it's overridden, it seems to leave behind the jsr311-api.jar. So if we exclude it, no one can attempt to use it, which looks to be the current problem

我得出的结论是,您需要做的就是将上述排除添加到 swagger 依赖项中。Hymanson 2.0 提供程序(取决于 JAX-RS 1)似乎被 2.4.1 提供程序(使用新版本)覆盖。所以我们不需要自己添加。当它被覆盖时,它似乎会留下jsr311-api.jar. 所以如果我们排除它,没有人可以尝试使用它,这看起来是当前的问题

<dependency>
    <groupId>com.wordnik</groupId>
    <artifactId>swagger-core_2.10</artifactId>
    <version>1.3.11</version>
    <exclusions>
        <exclusion>
            <groupId>javax.ws.rs</groupId>
            <artifactId>jsr311-api</artifactId>
        </exclusion>
    </exclusions>
</dependency>

回答by Koshan Imanga

You can change the tomcat version to 7 then it will run no need to change pom.xml

您可以将tomcat版本更改为7,然后它就可以运行了,无需更改pom.xml

回答by LeOn - Han Li

We are using jersey-json1.9 which has a dependency on jersey-corewhich also happen to have a javax.ws.rs.core.Applicationclass.

我们正在使用jersey-json1.9,它依赖于jersey-core它也恰好有一个javax.ws.rs.core.Application类。

So our fix is to exclude the jersey-corefrom jersey-json:

因此,我们的解决方法是排除球衣核心来自jersey-json

    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.9</version>
        <exclusions>
            <exclusion>
                <groupId>com.sun.jersey</groupId>
                <artifactId>jersey-core</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

回答by Walterwhites

the problem is related about "com.sun.jersey:jersey-core:jar:1.18.3" (or any 1.* version) because old versions of Jersey didn't have that "public Map getProperties()" method.

问题与“com.sun.jersey:jersey-core:jar:1.18.3”(或任何 1.* 版本)有关,因为旧版本的 Jersey 没有“public Map getProperties()”方法。

And one of your dependencies use Jersey version 1 (you can check that in running mvn dependency:tree and search all jersey-core version used)

并且您的一个依赖项使用 Jersey 版本 1(您可以在运行 mvn dependency:tree 并搜索所有使用的 jersey-core 版本时进行检查)

Me I have resolved the problem deleting all com.sun.jersey dependancies (old api) and using the new one org.glassfish.jersey API

我已经解决了删除所有 com.sun.jersey 依赖项(旧 api)并使用新的 org.glassfish.jersey API 的问题

https://mvnrepository.com/artifact/org.glassfish.jersey.core

https://mvnrepository.com/artifact/org.glassfish.jersey.core