[Jersey] Re: How to access a Spring bean from Jersey resource?

From: Petr Jurák <>
Date: Tue, 5 Apr 2011 12:40:22 +0200

There is an example "Spring annotations" in Jersey
You could find there some information. But in general it's better to
expose resource as a Spring bean and inject dependencies directly in
Spring context.


2011/4/5 Frederic Bergeron <>:
> Hi,
> I'm implementing a REST API on top of a web application using the Spring framework.  I managed to implement interoperability between Jersey and Spring using the following method:
> @GET
> @Produces( MediaType.APPLICATION_XML )
> public List<Item> getItems( @Context HttpServletRequest req ) {
>    HttpSession ses = req.getSession( true );
>    WebApplicationContext ctxt = WebApplicationContextUtils.getWebApplicationContext( ses.getServletContext() );
>    ItemCatalogImpl itemCatalog = (ItemCatalogImpl)ctxt.getBean( "itemCatalog" );
>    ...
> }
> My Jersey resource is able to access successfully a bean that has been instanciated by Spring.  Yeah!
> However now, I would like to implement unit tests for my REST API using InMemoryTestContainerFactory.  My solution fails when using InMemoryTestContainerFactory.  I think the InMemoryTestContainerFactory is not able to instanciate/simulate the HttpServletRequest and I cannot access the servlet context that is required to get the WebApplicationContext.
> Is there another way for my Jersey resource to access a Spring bean in both situations (real servlet and InMemoryTestContainerFactory)?  I suspect that there are many ways to do it but when I google about that, I'm a bit lost.  I have seen references to @Component, @Inject, @Autowire, and SpringServlet.  I have also seen that @Inject is deprecated in the 1.6's javadoc.  Which way is the best?  Any good links on this topic?
> Regards,
> Frederic Bergeron