NoSuchMethodError javax.ws.rs.core.Application.get

2019-06-05 08:02发布

问题:

I am trying my first REST application.

Below is my configuration:

  • Java 7
  • JBoss AS 7.1
  • Jersey 2.22.2

Below are list of jars downloaded by Maven:

<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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>org.prasad</groupId>
<artifactId>messenger</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>messenger</name>

<build>
    <finalName>messenger</finalName>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <inherited>true</inherited>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
    </plugins>
</build>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.glassfish.jersey</groupId>
            <artifactId>jersey-bom</artifactId>
            <version>${jersey.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <!-- use the following artifactId if you don't need servlet 2.x compatibility -->
        <!-- artifactId>jersey-container-servlet</artifactId -->
        <exclusions>
</exclusions>
    </dependency>
    <!-- uncomment this to get JSON support
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-moxy</artifactId>
    </dependency>
    -->
</dependencies>
<properties>
    <jersey.version>2.22.2</jersey.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

On server starting, I am getting the below exception:

21:14:29,013 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/messenger]] (MSC service thread 1-1) StandardWrapper.Throwable: java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:331) [jersey-server-2.22.2.jar:]
    at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:392) [jersey-container-servlet-core-2.22.2.jar:]
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:177) [jersey-container-servlet-core-2.22.2.jar:]
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:369) [jersey-container-servlet-core-2.22.2.jar:]
    at javax.servlet.GenericServlet.init(GenericServlet.java:242) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1202) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3655) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3873) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
    at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_79]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_79]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_79]

According to the logs above ApplicationHandler.init() method is calling Application.getProperties().

ApplicationHandler is in jersey-server-2.22.2.jar and Application is in javax.ws.rs-api-2.0.1, both jars are present in build path.

But still I am getting NoSuchMethodError. I have checked below links:

  • link 1 I am not using swagger at all;
  • link 2 I couldn't see more than one version of Application class;
  • link 3 I am using JRE 7 and JBoss AS 7.1 is also JDK 7 compatible.

So I have only one Application which contains getProperties() method and even then I am getting NoSuchMethodError. Can any one please give me reason why this is happening?

回答1:

As mentioned in the JBoss AS 7 documentation, the version of the JAX-RS specification used is 1.1.

Therefore, in order to use Jsersey 2.X, which provides support for JAX-RS APIs and serves both JAX-RS 1.x and JAX-RS 2.x reference implementations, you need to exclude the conflicts with RESTEasy by adding the following to your WEB-INF/jboss-deployment-structure.xml:

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <exclusions>
            <!-- Exclude RestEasy conflicts -->
            <module name="javaee.api" />
            <module name="javax.ws.rs.api"/>
            <module name="org.jboss.resteasy.resteasy-jaxrs" />
        </exclusions>
    </deployment>
</jboss-deployment-structure>

Also, you need to add the following to your web.xml:

<context-param>
    <param-name>resteasy.scan</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan.providers</param-name>
    <param-value>false</param-value>
</context-param>
<context-param>
    <param-name>resteasy.scan.resources</param-name>
    <param-value>false</param-value>
</context-param>

The related JBoss forum threads are the following:

  • Jersey 2 and JBoss 7
  • Jersey application on JBoss AS 7