Jersey using ContextResolver <JAXBContext> in Spring

So, I write Spring (2.5 (+ Jersey) (1.1.4.1) and try to create a JSONConfiguration using ContextResolver. Here is the code:

package com.rhigdon.jersey.config; import com.sun.jersey.api.json.JSONConfiguration; import com.sun.jersey.api.json.JSONJAXBContext; import javax.ws.rs.ext.ContextResolver; import javax.ws.rs.ext.Provider; import javax.xml.bind.JAXBContext; @Provider public final class JAXBContextResolver implements ContextResolver<JAXBContext> { private JAXBContext context; public JAXBContextResolver() throws Exception { this.context = new JSONJAXBContext(JSONConfiguration.mappedJettison().build(), "com.rhigdon.core.model."); } public JAXBContext getContext(Class<?> aClass) { return context; } } 

Unfortunately, my application still returns the default display:

{"id": "1", "question": "What is / was the name of your first pet?" }

When I debug an application, it never hits this code. Is this related to using SpringServlet? Here is my jersey configuration in my web.xml:

 <servlet> <servlet-name>Jersey Spring Web Application</servlet-name> <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Jersey Spring Web Application</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> 

Does anyone have a similar setup with JSONConfiguration?

+7
json spring jersey jaxb
source share
3 answers

You need to register your provider in the context of spring:

 <bean class="com.company.jersey.config.JAXBContextResolver"/> 

Or, if you are using annotation-based configuration, you need to annotate your provider class with @Component and include something like

 <context:annotation-config /> <context:component-scan base-package="com.company.jersey" /> 

into the context configuration of your application.

+12
source share

I use jersey version 1.10 and I don't have the @Component annotation or the bean definition and it works without it.

+1
source share


REST Jersey Service
com.sun.jersey.spi.spring.container.servlet.SpringServlet
com.sun.jersey.config.property.packages ca.gc.cbsa.ezfw.foundation.webservice 1

-4
source share

All Articles