Jackson FAQ: JAX-RS

1. General

Some useful links:

Also: the general JAX-RS way to register custom (configured) ObjectMapper can be seen here; this example just changes behavior so that "unknown properties" (JSON object properties that do not map to any property of matching POJO) do not throw exception but are just silently dropped:

// Customized {@code ContextResolver} implementation to pass ObjectMapper to use
@Provider
@Produces(MediaType.APPLICATION_JSON)
public class JacksonContextResolver implements ContextResolver<ObjectMapper> {
    private ObjectMapper objectMapper;

    public JacksonContextResolver() throws Exception {
        this.objectMapper = new ObjectMapper().configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
    public ObjectMapper getContext(Class<?> objectType) {
        return objectMapper;
    }
}

Many implementations have other mechanisms for injection; and even Jackson's own JAX-RS provider allows for direct configuring. But above mechanism should work for all compliant JAX-RS containers.

2. Working with specific impls

2.1 RESTeasy

(helpful information by Lars R-C, on Jackson mailing list, regarding how to deploy Jackson mapper to be used for JSON data binding with RESTeasy:

"For future JBoss/RESTEasy users: The answer is to have Jackson(Jaxb)JsonProvider in the resteasy.providers section of resteasy-jaxrs.war/WEB-INF/web.xml - it works like a beaut now!"

2.2 Jersey

Jersey version 1.4 added more convenient method for registering Jackson JAX-RS provider: see this javadoc for explanation on how to use FEATURE_POJO_MAPPING, which means "use Jackson for binding JSON data".

You can also enable it via Servlet init parameters, by adding:

<init-param>
  <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
  <param-value>true</param-value>
</init-param>

in your servlet definition XML file.

There are myriad other ways for configuration as well; for example:


CategoryJackson

JacksonFAQJaxRs (last edited 2011-04-01 17:54:47 by TatuSaloranta)

Copyright ©2009 FasterXML, LLC