Multiple JAX-RS URIs in One WAR

To deploy multiple JAX-RS applications with different URIs in one WAR you will have to create one javax.ws.rs.core.Application subclass per such an application (or use web.xml for this purpose). Obviously the in Java EE ubiquitous Convention over Configuration (or Configuration by Exception) cannot work any more: you will have to explicitly configure resources in each subclass by overriding the method getClasses or getSingletons:


@Path("first")
public class FirstResource {
    @GET
    public String first() {
        return "first";
    }
}


@ApplicationPath("one")
public class JAXRSConfigurationOne extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new HashSet<>();
        resources.add(FirstResource.class);
        return resources;
    }
}


@Path("second")
public class SecondResource {
    @GET
    public String first() {
        return "second";
    }
}


@ApplicationPath("two")
public class JAXRSConfigurationTwo extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new HashSet<>();
        resources.add(SecondResource.class);
        return resources;
    }
}

Both JAX-RS applications become accessible through distinct URIs: http://localhost:8080/multiple-roots/one/first and http://localhost:8080/multiple-roots/two/second

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting


NEW: Online Workhop Effective WebApps without Frameworks is also coming to: MUC Airport.

Airport MUC workshops: Web (SPA, PWAs, Offline, Desktop, Mobile) Applications Essentials and Effective Web Applications. No migrations. #usetheplatform

Podcast: airhacks.fm and newsletter: airhacks.news

A book about rethinking Java EE Patterns

Comments:

Not sure if you can have 2 Application subclasses in deployment for RESTEasy. On which JAX-RS implementation did you try this -Jersey ?

Posted by Rostislav Svoboda on May 07, 2015 at 10:30 PM CEST #

God bless you!

Posted by ivan on June 24, 2015 at 12:45 PM CEST #

Is there a similar mechanism for two ExceptionMappers and two ContextResolvers(JAXBContext) of those two different Applications within the same war?

Posted by Reinhard on July 10, 2015 at 09:14 AM CEST #

JAX-RS does not seem to allow equal @Path - even when only used in different @ApplicationPaths

Posted by Reinhard on July 10, 2015 at 10:07 AM CEST #

Have you tried to change the FirstResource and SecondResource path to the same value.
In this case, the response will be unexpected.
like:
http://localhost:8080/multiple-roots/one/newpath
and
http://localhost:8080/multiple-roots/two/newpath

They will return the same result.

Posted by li on February 24, 2016 at 08:49 AM CET #

@li I saw the same behavior but only in resteasy 2.x which was acknowledged as a bug https://issues.jboss.org/browse/RESTEASY-650

I'm yet to encounter the same on Jersey or Wink

Posted by Jesus on April 01, 2016 at 01:59 PM CEST #

This won't work in JBOSS AS. Following exception will be thrown at deployment : JBAS011232: Only one JAX-RS Application Class allowed

Posted by Ludovic Bertin on July 20, 2016 at 08:51 AM CEST #

Is there any way could I make JAXRSConfigurationTwo as the subresource of JAXRSConfigurationOne, like in the following URI

"http://localhost:port/one/two/second"

Posted by BalaajiChander on November 30, 2017 at 02:09 PM CET #

Hi. Is the example from Li (Feb 24th, 2016) meanwhile confirmed as working or not working with respect to JavaEE 8?

Posted by Stefan on August 24, 2018 at 10:59 AM CEST #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
realworldpatterns.com
...the last 150 posts
...the last 10 comments
License