java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map

2022-01-21 00:00:00 rest deployment java glassfish jersey

我正在尝试使用 jersey 2.17 部署我的第一个 REST 应用程序.

I am trying to deploy my first rest application using jersey 2.17.

我正在使用 Maven、GlassFish 3.1.2.2 进行部署.

I am using Maven, GlassFish 3.1.2.2 for deployment.

应用程序在 eclipse (tomcat) 中运行,但通过 glassfish 管理控制台部署时出现以下错误.

Application runs in eclipse (tomcat), but gives following error when deploying through glassfish admin console.

Error occurred during deployment: Exception while loading the app :
  java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: org.apache.catalina.LifecycleException:
  java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;. 
Please see server.log for more details.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>JerseyExample</groupId>
    <artifactId>JerseyExample</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <warSourceDirectory>WebContent</warSourceDirectory>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.17</version>
        </dependency>
    </dependencies>
</project>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>JerseyExample</display-name>
    <servlet>
        <servlet-name>MyJerseyExample</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>jerseyExample.resource</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>MyJerseyExample</servlet-name>
        <url-pattern>/example/*</url-pattern>
    </servlet-mapping>
</web-app>

推荐答案

好的,看看你的问题.您使用的 External Glassfish v 3.1.2.2 与 JAVA-EE 6 兼容.您在 Eclipse 中运行应用程序所使用的工具与 JAVA-EE 7 兼容.

Ok looked at your issue. The External Glassfish v 3.1.2.2 that you are using is JAVA-EE 6 compatible. And the one through which you are running your app in eclipse is JAVA-EE 7 compatible.

查看两个 java 文档:

Have a look at both the java docs:

EE6 的应用程序类 Java 文档

EE7 的应用程序类 Java 文档

您将看到 getProperties 方法已在 JAVA-EE 7 中引入.

You will see that the getProperties method got introduced in JAVA-EE 7.

只需升级到与 JAVA-EE 7 兼容的 glassfish 版本 4.x.假设没有其他问题应该是好的.

Simply upgrade to glassfish version-4.x that is compatible with JAVA-EE 7. Assuming nothing else breaks should be good.

相关文章