Adam Bien's Weblog

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 workshop: HTML 5 and JavaScript Essentials, Three days in April 2017: from Java EE 7 Architectures over Microservices to Performance, Troubleshooting and Monitoring
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.

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 #

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