I'm using Jersey 2.4 to create a simple REST interface that serves up a JSON object. My problem is that I'm trying to use the fasterxml Jackson annotations to control the output and this is not working for me. I have put the annotations into my bean class but they are ignored.
When I explicitly create an ObjectMapper and use this to stringify the Java bean, I get the output that I want, which respects the Jackson annotations. However, I would prefer that I don't have to do this step so that my resource class can simply return the bean and the Jersey framework takes care of stringifying it.
I have tried to solve this using the answer from Custom ObjectMapper with Jersey 2.2 and Jackson 2.1, however, this does not appear to work for me. I see that the ContextResolver is created but it is never called.
I have also spent many hours trying to solve this apparently simple problem. I have stripped this down to a very simple test case, which is shown below. I would appreciate any help at all in resolving this.
Resource Java class:
@Path("resource")
public class MainResource {
public static class Foobar {
@JsonIgnore
private String foo = "foo";
private String baa = "baa";
private Map<String, List<? extends Number>> map = new HashMap<>();
public Foobar() {
map.put("even", Arrays.asList(new Integer[] { 2, 4, 6, 8, 10 }));
map.put("odd", Arrays.asList(new Integer[] { 1, 3, 5, 7, 9 }));
map.put("float", Arrays.asList(new Float[] { 1.1F, 2.2F, 3.3F }));
}
public String getFoo() {
return foo;
}
public void setFoo(String foo) {
this.foo = foo;
}
public String getBaa() {
return baa;
}
public void setBaa(String baa) {
this.baa = baa;
}
@JsonAnyGetter
public Map<String, List<? extends Number>> getMap() {
return map;
}
public void setMap(Map<String, List<? extends Number>> map) {
this.map = map;
}
}
private ObjectMapper om = new ObjectMapper();
@GET
@Path("get-object")
@Produces(MediaType.APPLICATION_JSON)
public Foobar getObject() {
// In this method, I simply return the bean object but the WRONG JSON syntax is generated.
return new Foobar();
}
@GET
@Path("get-string")
@Produces(MediaType.APPLICATION_JSON)
public String getString() throws JsonProcessingException {
// This method returns the RIGHT JSON syntax but I don't want to have to explicitly use the ObjectMapper.
Foobar foobar = new Foobar();
return om.writeValueAsString(foobar);
}
}
web.xml:
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<module-name>sample</module-name>
<servlet>
<servlet-name>Jersey Web Application</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>ie.cit.nimbus.sample</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
POM dependencies:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-spring3</artifactId>
<version>2.4.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.4.1</version>
</dependency>
</dependencies>