Jersey 2.1 + JBoss 7.1 NoSuchMethodError: getProperties

懵懂的女人 提交于 2019-12-03 14:59:45

In short, significant structural changes have been made between 1.17 and 2.2 of the Jersey implementation. You need to:

1) Extend your application class from "org.glassfish.jersey.server.ResourceConfig"

2) Add the packages line within the application class constructor, like so:

    public class MyApplication extends ResourceConfig {
        public MyApplication() {
            packages("com.mysite.MyApplication");
        }
    }

3) Within web.xml, you'll need to update a number of places. See below:

    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>com.mysite.MyApplication</param-value>
    </init-param>

4) Deploy and have fun.

I know this thread is not that fresh, but still, I only hit this problem yesterday and nothing seems to work.

My scenario is very similar: Jersey 2.23.1 REST App (JAX-RS 2.x) already running on tomcat and need to run on JBoss 7.1.1 (built-in JAX-RS 1.1).

  1. Turn off Restease package scanning in 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>
    
  2. Remove all tags with "jaxrs" from standalone.xml. Otherwise you will still face LinkageError because JBoss keeps 1.1 spec on.

  3. Create yourApp.war!WEB-INF\jboss-deployment-structure.xml just like is pointed out here: https://docs.jboss.org/author/display/AS7/Class+Loading+in+AS7#ClassLoadinginAS7-JBossDeploymentStructureFile

This way, not only:

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

Disapears, but also, JAXB works fine (No ClassNotFoundException for javax.xml.bind.JAXBException once module javax.xml.bind.api is also activated).

Obs1: The original question is mixing jersey 1.x with jersey 2.x. There is no PojoMappingFeature in jersey 2.x and base package is org.glassfish.jersey. Take a look at https://jersey.java.net/documentation/latest/migration.html#mig-1-x-json

Obs2: I have also tried other approaches like extending ResourceConfig and scanning packages from there or registering classes directly. Nothing worked like the proper documentation in item 3. So kept my servlet untouched:

    <servlet>
      <servlet-name>WebNize REST Service</servlet-name>
      <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
      <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>br.com.webnize.rest.service</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
      <servlet-name>WebNize REST Service</servlet-name>
      <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

I hope it helps others!

Additionally, it seems that the practice of using wildcards within the java source is deprecated.

Not this)

    @Path("/v1/status/*")

But this)

    @Path("/v1/status")

An improvement, IMHO.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!