简体   繁体   中英

Is it possible to expose JAX RS classes inside jars without declaring them in web.xml / Application subclass?

we're trying to create an .ear project with a web application exposing some rest api. The project includes "micro jars" representing some businness logic (annotated by @Path ), each jar contains some rest class (annotated by @Path ). The application is Java EE 7 compliant and is deployed on a WAS 9 instance, the structured .ear is the following one:

project.ear
|
|- META-INF
|    |- application.xml
|
|-lib
|    |- service1.jar                (with rest classes)
|    |              |- META-INF
|    |                      |- beans.xml
|    |              
|    |              
|    |
|    |- service2.jar  (with rest classes, same structure as service1.jar)
|    |   
|    |- core.jar      (with Application subclass, same structure as service1.jar)
|    
|    
|- app.war
|    |- classes (empty)
|    |- web.xml
|    |- beans.xml

The web.xml in .war package contains a ref to the custom application subclass:

<servlet>
  <description>JAX-RS Tools Generated - Do not modify</description>
  <servlet-name>JAX-RS Servlet</servlet-name>
  <servlet-class>com.ibm.websphere.jaxrs.server.IBMRestServlet</servlet-class>
  <init-param>
    <param-name>javax.ws.rs.Application</param-name>
    <param-value>com.example.MyApplicationSubclass</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>


<servlet-mapping>
  <servlet-name>JAX-RS Servlet</servlet-name>
  <url-pattern>/resources/*</url-pattern>
</servlet-mapping>

And every bean.xml looks like this:

<?xml version="1.0" encoding="UTF-8"?>
 <beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
     http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
     bean-discovery-mode="all">
 </beans>

MyApplicationSubclass is empty, it does have any overridden method and is annotated with the basic @ApplicationPath annotation.

On other AS like WebLogic and JBOSS there's no need of explicit servlet declaration, everything is discovered automatically, but on WAS we found these main problems:

  1. The autodiscovery is completely off, neither @ApplicationPath and @Path classes are auto discovered.
  2. The MyApplicationSubclass must be included in the web.xml of the.war
  3. Even if the MyApplicationSubclass is declared in the web.xml, the rest endpoints are not discovered automatically
  4. Even through reflection, from MyApplicationSubclass is not possible to find rest enpoint classes

Any tips? Why this structure is ok on other AS? Are these problems related to Apache CXF?

NOTE: we've also tryied to start the application with parent last loading policy, but the issue remains.

The problem is most likely that the application classes are not packaged inside the WAR file. Per section 2.3.2 of the JAX-RS spec (I'm using the JAX-RS 2.1 spec, but I believe the same rules apply in 2.0):

A JAX-RS application is packaged as a Web application in a.war file. The application classes are packaged in WEB-INF/classes or WEB-INF/lib and required libraries are packaged in WEB-INF/lib.

It sounds like WebSphere takes this line more literally than some of the other application servers that you experimented with.

So, the easiest, most assured fix would be to package the JAX-RS application classes in the WAR's WEB-INF/classes or in a JAR in the WEB-INF/lib directory instead of the EAR's lib directory.

Another option (which I have not tested myself) might be to add a Class-Path: attribute to your WAR's manifest file to explicitly reference the JARs containing the JAX-RS application - iiuc, that should put those JARs on the same classpath as the rest of the web module, allowing it to be loaded.

A third option (also which I have not tested) might be to put the application classes into a shared library and then associate that shared library with the web module.

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM