1

I want to incorporate/add a Jersey project B (already working well) into a new Jersey project A, that would act as a filter/security layer. So as a basic step, I added dependency to Project B on the build path of Project A and also added the same to the Deployment Assembly in the build path. I understood from this post, that I could do so by having the servlets in the same web.xml and mapping them differently using <servlet-mapping> . I did not have any luck when I was trying to access the resource of Project B.

web.xml

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>org.abc.def.ba, org.pqr.xyz</param-value>
    </init-param>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>org.abc.def.ba.CustomApplication</param-value>
    </init-param> 
    <load-on-startup>1</load-on-startup>
</servlet>

 <servlet-mapping>
    <servlet-name>Jersey Web Application</servlet-name>
    <url-pattern>/webapi/*</url-pattern>
</servlet-mapping>

<!-- The Servlet of Project B -->
<servlet>
    <servlet-name>Jersey Web Application 2</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-     class>

<init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value> org.pqr.xyz.MyApplication</param-value>
    </init-param> 

    <!-- Register resources and providers under my.package. -->
   <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value> org.pqr.xyz</param-value>
    </init-param>

    <!-- Register my custom provider (not needed if it's in my.package) AND LoggingFilter. -->
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value> org.pqr.xyz.mapper.ObjectMapperProvider</param-value>
    </init-param>

     <init-param>
        <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
        <param-value>true</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value> org.pqr.xyzr</param-value>
    </init-param>
    <init-param>
        <param-name>com.sun.jersey.spi.container.ResourceFilters</param-name>
        <param-value>com.porterhead.rest.filter.ResourceFilterFactory</param-value>
    </init-param>
    <init-param>
        <param-name>readOnly</param-name>
        <param-value>false</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey Web Application 2</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

So, when I try to access http://localhost:8080/ba/webapi/myresource, it works well. But when I try http://localhost:8080/ba/rest/newresource, I get a 404 error. I know I'm missing out something. I appreciate your guidance on this.

Community
  • 1
  • 1
vardhinisuresh27
  • 371
  • 2
  • 6
  • 18

1 Answers1

0

If you are using servlet 3.0, then you can use @ApplicationPath annotation to use different application paths for different resources. In case you do not want to use the annotation, you can achieve the same using web.xml as below

<web-app>
<servlet>
    <servlet-name>org.foo.rest.MyApplication</servlet-name>
</servlet>
...
<servlet-mapping>
    <servlet-name>org.foo.rest.MyApplication</servlet-name>
    <url-pattern>/resources</url-pattern>
</servlet-mapping>
...

For details please refer Jersey documentation for deployment options.

Rohan
  • 179
  • 2
  • 3
  • I'm doing the same thing, pls refer to my code. And to add, when I am trying to build and run the Tomcat server, I get the `java.lang.ClassNotFoundException: org.pqr.xyzr.MyApplication`. Do u have any idea the reason for this ? – vardhinisuresh27 May 10 '16 at 08:51
  • In your web.xml the second project is commented. Can you upload the latest version of your web.xml? – Rohan May 10 '16 at 08:59
  • It's not commented, its because of the usage of "/*" which is inturn commenting the code following. The code posted is the current web.xml. Thanks! – vardhinisuresh27 May 10 '16 at 09:06
  • You have added the package org.pqr.xyz in first servlet block also.Is that correct? – Rohan May 10 '16 at 09:27
  • I've removed it, still its the same. My idea of doing this is to have an additional security layer, that does authentication to get access to the inner layer (implementation in Project B). So I want any endpoint to be caught/intercepted by Project A and re-routing it to the inner/Project B's resource that was requested.Any idea on how this could be done? – vardhinisuresh27 May 10 '16 at 09:43
  • For security you should use filters. I would suggest you to use Apache Shiro or any other security framework for making your application secure. – Rohan May 12 '16 at 13:16