dev@jersey.java.net

Re: [Jersey] Combining Jersey and non-Jersey on one webapp

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 05 Jan 2009 13:14:38 +0100

Hi Frank, Craig,

Do you think it would be useful to have support for the case of
dispatching to Jersey and if the request is not dispatched then it is
dispatched to Spring?

Craig, what would be the best way to utilize the Servlet-based
components in this respect, as filters? does it make sense to split
out Jersey config functionality using some context?

Paul.

On Jan 3, 2009, at 1:24 AM, Craig McClanahan wrote:

> Frank McLean wrote:
>>
>> Hi,
>>
>> This is kind of a user question, but I think only a developer could
>> answer it. My apologies if this is not the case.
>>
>> I have a project using Spring MVC to which I would like to add
>> some services. I have had trouble separating the 2 ‘streams’ –
>> either all requests are taken by Spring MVC or all by Jersey
>> (usually the latter). After reading a lot and much trial and
>> error, the following web.xml seems to do the trick:
>>
>> <servlet>
>> <servlet-name>springmvc</servlet-name>
>> <servlet-class>
>> org.springframework.web.servlet.DispatcherServlet
>> </servlet-class>
>> <load-on-startup>1</load-on-startup>
>> </servlet>
>>
>> <servlet>
>> <servlet-name>jersey</servlet-name>
>> <servlet-class>
>>
>> com.sun.jersey.spi.container.servlet.ServletContainer
>> </servlet-class>
>> <load-on-startup>2</load-on-startup>
>> </servlet>
>>
>> <servlet-mapping>
>> <servlet-name>springmvc</servlet-name>
>> <url-pattern>*.htm</url-pattern>
>> </servlet-mapping>
>>
>> <servlet-mapping>
>> <servlet-name>jersey</servlet-name>
>> <url-pattern>/myservices/*</url-pattern>
>> </servlet-mapping>
>>
>> The only thing I have to remember in this situation is that the
>> servlet-mapping for ‘jersey’ effectively creates a new ‘context
>> root’, so that if I have a resource class as follows:
>>
>> @Path("/services/test")
>> public class TestService {
>>
>> @GET
>> @Produces("text/plain")
>> @Path("dosomething")
>> public String someMethod(etc…)
>>
>> Rather than having the service as –
>>
>> http://myhost/mywebapp/services/test/dosomething
>>
>> I have to use –
>>
>> http://myhost/mywebapp/myservices/services/test/dosomething
>>
>> Once I do that, all is well.
>>
>> OK, so, my question is: Is this a valid approach, or a kludge? Am
>> I skating on thin ice, simply exploiting a ‘feature’ of the
>> implementation which may at some point be refactored away?
>>
>> Thanks for your time,
>>
>> Frank McLean
>>
>>
> This kind of question would indeed be welcome on the user list, as
> it's about *using* Jersey instead of developing it :-).
>
> Your approach will work, as long as the "*.htm" pattern covers *all*
> the resources you would ever want to have returned via Spring MVC,
> and you'll *never* want such a call to go through Jersey instead.
> That of course depends on your application requirements, but it
> seems like something I could live with using this combination of
> technologies.
>
> Craig
>