Question

I have a web application with the following folder structure

ProjectName 
     |.. src/main/java
       |.. folder1
         |.. restEndpoint1.java , restendpoint2.java etc 

with the restendpoint1.java being like

@Path("/static1/static2")
public class DoStuff {

@POST
@Path("/static3")
@Consumes(MediaType.APPLICATION_XML)
@Produces("application/xml")
public Response validation(String inputXML){

my web.xml is :

<servlet>
        <servlet-name>jersey-servlet</servlet-name>
        <servlet-class>
            com.sun.jersey.spi.container.servlet.ServletContainer
        </servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>folder1</param-value>
        </init-param>
        <init-param>
            <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
            <param-value>true</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/folder1/*</url-pattern>
    </servlet-mapping>

Which makes my URL

http:\\localhost:port/projectName/foldername/{restendpoint1.path}

Is there a way I can eliminate the foldername from the mapping so as I can make my mapping

localhost:port/projectName/{restendpoint1.path}

How do I manipulate my web.xml to achieve this .

The reason I need to do this is , I need the folder structure to ensure maintainability of the code ( as opposed to grouping everything into one class and making the web.xml map to that class) but I also have to avoid getting the foldername in the mapping .

Was it helpful?

Solution

Don't put folder1 in the url-pattern. You've already put folder1 in the packages being scanned for services. By putting folder1 in the url-pattern, you are indicating that you want it to be in the URL. Put whatever you want the URL to be in there. So

<servlet-mapping>
     <servlet-name>jersey-servlet</servlet-name>
     <url-pattern>/*</url-pattern>
</servlet-mapping>

This should make all calls go to your rest services. However, you should probably put some path in there because you don't want to map all of your URLs to REST services (since there will be other URLs that are not REST services). Maybe something like

<servlet-mapping>
     <servlet-name>jersey-servlet</servlet-name>
     <url-pattern>/services/*</url-pattern>
</servlet-mapping>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top