2

I have java application (myapp) which has say following three rest endpoints

  1. localhost:8080/persons
  2. localhost:8080/languages
  3. localhost:8080/countries

My requirement is I need to deploy 3 instances of the same application say myapp1, myapp2, myapp3. myapp1 should allow only /persons endpoint myapp2 should allow only /languages endpoint myapp3 should allow only /countries endpoint

To achieve this i prefer not to have much of java code change. Is there any possibility to achieve via web.xml or springsecurity.xml or something of similar?

My java application is using jersey framework.

Following is web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
    <display-name>ResourcesAPI</display-name>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring-security.xml</param-value>
    </context-param>
    <servlet>
        <servlet-name>PersonDataService</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.local.service.PersonDataApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>PersonDataService</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <filter-mapping>
        <filter-name>request-logging</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>
3
  • Please share some more info regarding how you are integrating your app with Jersey. Is it an embedded server? Are you using Tomcat and deploying a war? share your existing web.xml etc Commented Feb 17, 2017 at 10:06
  • @gba, Deploying as war using Tomcat server. added my web.xml to the question. Commented Feb 18, 2017 at 15:19
  • See my last edits. There are 2 options. Commented Feb 19, 2017 at 11:57

1 Answer 1

6

If I understand correctly you are returning the list of service classes in com.local.service.PersonDataApplication.

You could take a parameter from the command line running tomcat as a system property, and decide what classes to return in com.local.service.PersonDataApplication::getClasses method.

Assuming you have a service class for each endpoint, which makes sense anyway...

Assuming you are starting tomcat with the script catalina.sh you can do something like:

export JAVA_OPTS=-DpersonsEndpoint=true
catalina.sh

And in your getClasses method:

if(System.getProperty("personsEndpoint") != null && System.getProperty("personsEndpoint").equals("true")) {
    ... return the person endpoint class
}

EDIT:

Another option

I do not know why you have chosen to have a custom Application class (It might make sense to your usage), but if you could drop it, you could use jersey.config.server.provider.classnames in your web.xml to define what classes to scan, and then you could do your changes only in the web.xml.

In one config:

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>
        com.local.service.PersonService
    </param-value>
</init-param>

In another config:

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>
        com.local.service.LanguagesService
    </param-value>
</init-param>

Also here the assumption is that the classes are separated.

com.local.service.PersonService - the class where all your jaxrs annotated methods are regarding the /persons endpoint.

Sign up to request clarification or add additional context in comments.

1 Comment

i have followed your option 1 and its works fine. Thanks @gba for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.