Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey implementing ContextResolver<JAXBContext> in Spring

So I am writing a Spring(2.5( + Jersey(1.1.4.1) and trying to create a JSONConfiguration using a 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 app is still returning the default mapping:

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

When I debug the application it never actually hits this code. Is this due to using the SpringServlet? Here is my Jersey Config 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>

Anyone have a similar setup with JSONConfiguration working?

like image 416
rhigdon Avatar asked Dec 21 '09 21:12

rhigdon


2 Answers

You need to register your provider in your spring context:

<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" />

to your application context configuration.

like image 151
rhigdon Avatar answered Nov 16 '22 07:11

rhigdon


I'm using jersey version 1.10 and I don't have the @Component annotation nor the bean definition, and it works without it.

like image 1
Elad Tabak Avatar answered Nov 16 '22 08:11

Elad Tabak