Java RestFull WebService: JAX-RS implementation with Jersey 2.3.1 libraries
Asked Answered
P

5

26

I am trying to run a simple "Hallo World" application Jersey 2.3.1 REST service on JBoss jboss-eap-6.1 AS. In web.xml i have disabled restEasy library. During deployment i am getting the error:

JBWEB000289: Servlet com.sun.jersey.samples.helloworld.resources.MyApplication threw load() exception: java.lang.NoSuchMethodError: javax.ws.rs.core.Application.getProperties()Ljava/util/Map;

In POM i put these dependencies:

<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet-core</artifactId>
    <version>2.3.1</version>
</dependency>
<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
</dependency>

This is my web.xml with restEasy tags disabling:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>com.sun.jersey.samples.helloworld.resources.MyApplication</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.sun.jersey.samples.helloworld.resources.MyApplication</param-value>
        </init-param>
           <load-on-startup>1</load-on-startup>
    </servlet>
    <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>
    <servlet-mapping>
        <servlet-name>com.sun.jersey.samples.helloworld.resources.MyApplication</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

And my resource config java class:

package com.sun.jersey.samples.helloworld.resources;
import org.glassfish.jersey.server.ResourceConfig;
public class MyApplication extends  ResourceConfig {   

     public MyApplication() {
            packages("com.sun.jersey.samples.helloworld.resources");
          //super(HelloWorldResource.class);

     }
}

Someone have any idea to solve it? thanks in advance, Roberto

Portia answered 7/10, 2013 at 13:1 Comment(1)
Probably you want to refrain from using com.sun.jersey.samples.helloworld.resources as the package for your own files... com.sun may not be yours... just a guess.Planck
R
50

NoSuchMethodError usually means you are having two different versions of the class on your classpath. As the javax.ws.rs.core.Application class does have the getProperties() method in its JAX-RS 2 version, but not in JAX-RS 1.x, I would guess that somehow you are combining the old 1.x Jersey (or old REST api) with the current (2.3.1) one.

Also the package you are working in (com.sun.jersey - the 'old' Jersey package) points a bit towards this direction (although just placing your code into that package itself cannot cause the mentioned problem), you obviously started with the Jersey 1.x example as a base (there are samples in Jersey 2 as well, see helloworld-webapp on Jersey GitHub).

Is it possible, that restEasy (also definitely containing the javax.ws.rs.core.Application class) is not completely switched off and somehow defaults to JAX-RS 1.x version?

I would start with inspecting your pom file, look at the effective pom (if your project descriptor has some parent) and check carefully what is on your classpath - I believe there is a 1.x version of javax.ws.rs-api somewhere. Also try to clean all the compiled stuff and rebuild from scratch.

Speaking of dependencies, if your list is exhaustive (regarding Jersey), you will most likely have to add jersey-common (2.3.1) dependency, as already during the initialization, the ResourceConfig.packages() method calls the PackageScanner constructor, which contains call to ReflectionHelper - and this is not a part of the server jar any more.

Hope this helps.

Reformed answered 9/10, 2013 at 0:38 Comment(3)
Hi,thanks for your interest,i tried to add maven dependency jersey-common (2.3.1) as you suggest,also i tried to disable RESTeasy removing the lines: <extension module="org.jboss.as.jaxrs"/> and <subsystem xmlns="urn:jboss:domain:jaxrs:1.0"/> in either configuration file: <JBoss_Home>/standalone/configuration/standalone.xml and <JBoss_AS_7_Home>/domain/configuration/domain.xml but nothing has canched...probably as you suggest, jboss dont switch off RESTeasy...Portia
For those who had problems by using com.sun.jersey, look at org.glassfish.jersey for Jersey 2.x packages.Exergue
If using gradle, you may enter this section: configurations.all { resolutionStrategy.dependencySubstitution { //This solves the issue with different gradle package names for the same set of classes implementing (different versions of) JSR-311. substitute module('javax.ws.rs:jsr311-api') with module('javax.ws.rs:javax.ws.rs-api:2.0.1') } }Rhettrhetta
P
18

I faced same problem recently. I thought share my steps for you. As the other answers state, the problem is mainly because of having two different versions of the same class on your classpath. So when you add maven dependencies in your pom be careful.

These kind of problems are normally called as Jar Hell. You can use jhades API for investigate the classes overlap one another. Here is the simple steps i have followed.

Add jhades dependency into your pom.

<dependency>
    <groupId>org.jhades</groupId>
    <artifactId>jhades</artifactId>
    <version>1.0.4</version>
</dependency>

Show the report

Call new JHades().overlappingJarsReport(); in your main method, it will output to stdout.

Sample Output:

file:/Users/justin/.m2/repository/javax/ws/rs/jsr311-api/1.1.1/jsr311-api-1.1.1.jar overlaps with
file:/Users/justin/.m2/repository/javax/ws/rs/javax.ws.rs-api/2.0/javax.ws.rs-api-2.0.jar - total overlapping classes: 55 - same classloader ! This is an ERROR!

Remove one of overlap maven dependency in your pom.

Also you can use another approach like maven's dependency exclusions.

Source: Blog post on jhades

Hope this will help someone :)

Parlormaid answered 6/11, 2015 at 7:28 Comment(6)
Just to build on this, the next step it run run a dependency tree. Use . mvn dependency:tree > deps.txt. Then search the text file for the jar that was flagged by Jhades.Interdenominational
Hi I have same problem. I am using Jersey2.22.2 and I don't have any other jar file in my class path other that Jersey. I am getting the same error. I ran the Jhades the report does not say anything about Jersey multiple classes. I am using apache-tomee-plus-1.7.4 server. any suggestions?Spano
@Philip PJ normally this will happens due to when dependencies are overlap each other.in your situation there would be also.if this approach doesn't help you.you have to find a another approach.I am sorry i amnot much familiar with apache tomee.maybe this will help you. #13781360. if you fix your issue please post ur answer here then it will help for some one.Parlormaid
also try this #30662777Parlormaid
I configured Glassfish and it is running fine. I am happy with Glassfish to test my REST web services. Thanks for the supportSpano
jhades.org is out of dateSic
R
14

Just got this working on JBoss EAP 6.1.1 - Jersey 2.3.1.

The usual things don't seem to work/are not enough on their own:

  • disabling the jaxrs-subsystem in standalone.xml/domain.xml
  • or, excluding the jax-rs modules in jboss-deployment-structure.xml

Additionaly you need to disable the loading of the jax-rs 1.1 API completely by modifying module.xml in jboss-eap-6.1/modules/system/layers/base/javax/ws/rs/api/main/module.xml like this:

<module xmlns="urn:jboss:module:1.1" name="javax.ws.rs.api">
<resources>
    <!-- Disable the next line -->
    <!-- resource-root path="jboss-jaxrs-api_1.1_spec-1.0.1.Final-redhat-2.jar"/ -->
    <!-- Insert resources here -->
</resources>

<dependencies>
    <module name="org.jboss.resteasy.resteasy-jaxrs" services="export"/>
</dependencies>
</module>

Please note that this will disable the jax-rs implementation of JBoss (RestEasy) for all other applications as well (as does disabling the jaxrs subsystem in standalone/domain.xml).

Rambow answered 16/10, 2013 at 13:30 Comment(3)
This solved my problem. In my case, the path was little different to module.xml. It was like, jboss-as-7.1.1.Final\modules\javax\ws\rs\api\main\module.xmlAbash
This solution also solved my problem when trying to call a external rest service so the client side is running on JBoss EAP 6.4 (and packed on a EAR file - no web application)Multistage
Is there any way without modifying module.xml ?Mckinleymckinney
A
4

This is a Jersey version conflict problem. I had the same problem. Here is how it is resolved:

  1. See your package dependencies "mvn dependency:tree"

  2. If there is a library dependency that depends on an old Jersey version, you could add an exclusions section in the dependency tag for that library in pom.xml

Aureole answered 24/3, 2017 at 20:41 Comment(0)
S
1

Using mvn dependency:tree (thanks for suggestion above) I was able to identify that the culprit (in my case) was: javax.ws.rs:jsr311-api:1.1 Removing this dependency solved my problem.

Streaming answered 27/9, 2017 at 20:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.