title: Using Jackson ObjectMapper with Jersey
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>I have tried to solve this using the answer from <a href="https://stackoverflow.com/questions/18872931/custom-objectmapper-with-jersey-2-2-and-jackson-2-1">Custom ObjectMapper with Jersey 2.2 and Jackson 2.1</a>, however, this does not appear to work for me. I see that the ContextResolver is created but it is never called.</p>
  
  <p>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.</p>
  
  <p>Resource Java class:</p>
  
  <pre><code>@Path("resource")
  public class MainResource {
  
      public static class Foobar {
          @JsonIgnore
          private String foo = "foo";
          private String baa = "baa";
          private Map&lt;String, List&lt;? extends Number&gt;&gt; map = new HashMap&lt;&gt;();
  
          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&lt;String, List&lt;? extends Number&gt;&gt; getMap() {
              return map;
          }
  
          public void setMap(Map&lt;String, List&lt;? extends Number&gt;&gt; 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);
      }
  }
  </code></pre>
  
  <p>web.xml:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;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"&gt;
  
      &lt;module-name&gt;sample&lt;/module-name&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;Jersey Web Application&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
              &lt;param-value&gt;ie.cit.nimbus.sample&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Jersey Web Application&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>POM dependencies:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.jaxrs&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-jaxrs-json-provider&lt;/artifactId&gt;
          &lt;version&gt;2.3.0&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.ext&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-spring3&lt;/artifactId&gt;
          &lt;version&gt;2.4.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.glassfish.jersey.media&lt;/groupId&gt;
          &lt;artifactId&gt;jersey-media-json-jackson&lt;/artifactId&gt;
          &lt;version&gt;2.4.1&lt;/version&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>