1

I use spring-jersey to expose rest services. My web.xml looks as follows:

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Let say standard. I have a lot of rest services in many packages and I need to goup them in two context, let say "base" and "advanced" services. Moreover I need to get rid of the "rest" prefix in url-pattern. So I thought about group them into two packages and then in web.xml define two jersey servlets with com.sun.jersey.config.property.packages init param:

<servlet>
    <servlet-name>Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.example.app.rest.base</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>Jersey REST Service</servlet-name>
    <url-pattern>/base/*</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>Another Jersey REST Service</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>com.example.app.rest.advanced</param-value>
    </init-param>
</servlet>
<servlet-mapping>
    <servlet-name>Another Jersey REST Service</servlet-name>
    <url-pattern>/advanced/*</url-pattern>
</servlet-mapping>

Unfortunately due to component scan set in applicationContext.xml

<context:component-scan base-package="com.example.app" />

property com.sun.jersey.config.property.packages is ignored (all rest services can be accessed under each context) and it cannot be handle like that.

I am wondering how can I deal with that in other way. The only thing which I don't want to do is to set

<url-pattern>/*</url-pattern>

that catch everyting.

Espeen
  • 73
  • 2
  • 11

1 Answers1

1

You can specify multiple packages. Just separate them by a comma.

<context:component-scan base-package="com.example.app,com.sun.jersey" />

You can also define two component-scan items and they should work just as well, too.

If the package differentiation won't help, use a filter.

<context:component-scan base-package="org.example">
      <context:include-filter type="regex" expression=".*Repository"/>
   </context:component-scan>

And then, just make a separate applicationContext for each service but use an init-param of contextConfigLocation and init-value of the location/name of that app context. So, if you make a specific app context to load for each service, the component scan filtering will load everything you need for one service and exclude the other one.

<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/rest-service1.xml</param-value>
</init-paraam>

Actually, if you make a separate xml file for each, you can just go back to using the different package at that point, I believe.

AHungerArtist
  • 9,332
  • 17
  • 73
  • 109