title: Jackson Provider Multiple ObjectMapper for same Class
body: <p>I want to serialize the same Category class with two mapper in different resources method.<br/>
  I have written two classes that serialized Category in two different ways
  <strong>CategorySerialized</strong> and <strong>CategoryTreeSerialized</strong></p>
  
  <pre><code>public class MyJacksonJsonProvider implements ContextResolver&lt;ObjectMapper&gt;
  {
      private static final ObjectMapper MAPPER = new ObjectMapper();
  
      static {
          MAPPER.enable(SerializationFeature.INDENT_OUTPUT);          
          MAPPER.registerModule(new SimpleModule()
                  .addSerializer(Category.class, new CategorySerializer(Category.class)));  
          }
  
      public MyJacksonJsonProvider() {
          System.out.println("Instantiate MyJacksonJsonProvider");
      }
  
      @Override
      public ObjectMapper getContext(Class&lt;?&gt; type) {
          System.out.println("MyJacksonProvider.getContext() called with type: "+type);
          return MAPPER;
      }
  </code></pre>
  
  <p>this is the simple entity Category</p>
  
  <pre><code>   @Entity
     public class Category {
  
          @Id
          @GeneratedValue(strategy = GenerationType.IDENTITY)
  
          @Type(type = "objectid")
          private String id;
          private String name;
  
          @ManyToOne
          @JsonManagedReference
          private Category parent;
  
          @JsonBackReference
          @OneToMany(mappedBy = "parent", fetch = FetchType.EAGER)
          @Column(insertable = false)
          private List&lt;Category&gt; children;
  
          ....getter and setter ....
      }
  </code></pre>
  
  <p>this is the CategoryResource</p>
  
  <pre><code>@Path(value = "resource")
  public class CategoryResource {
  
      @Inject
      CategoryService categoryService;
  
      @Context
      Providers providers;
  
      @GET
      @Produces(value = MediaType.APPLICATION_JSON+";charset="+ CharEncoding.UTF_8)
      @Path("/categories")
      public List getCategories(){
          List&lt;Category&gt; categories = categoryService.findAll();
          return categories;
      }
  
      @GET
      @Produces(value = MediaType.APPLICATION_JSON+";charset="+ CharEncoding.UTF_8)
      @Path("/categoriestree")
      public List getCategoriesTree(){
          List&lt;Category&gt; categories = categoryService.findAll();
  
          ContextResolver&lt;ObjectMapper&gt; cr = providers
                  .getContextResolver(ObjectMapper.class, MediaType.APPLICATION_JSON_TYPE);
          ObjectMapper c = cr.getContext(ObjectMapper.class);
          c.registerModule(new SimpleModule()
                  .addSerializer(Category.class, new CategoryTreeSerializer(Category.class)));
  
          return categories;
      }
  </code></pre>
  
  <p><strong>CategorySerialized</strong> extends StdSerializer is registered with the provider</p>
  
  <pre><code>MAPPER.registerModule(new SimpleModule()
                      .addSerializer(Category.class, new CategorySerializer(Category.class))); 
  </code></pre>
  
  <p><strong>CategoryTreeSerialized</strong> extends StdSerializer is registered within the resources</p>
  
  <pre><code>ContextResolver&lt;ObjectMapper&gt; cr = providers
                      .getContextResolver(ObjectMapper.class, MediaType.APPLICATION_JSON_TYPE);
              ObjectMapper c = cr.getContext(ObjectMapper.class);
              c.registerModule(new SimpleModule()
                      .addSerializer(Category.class, new CategoryTreeSerializer(Category.class)));
  </code></pre>
  
  <p>Unfortunately this does not work because mapper is static final.<br/>
  The first resource called, register the module and then does not change</p>
  
  <p>For example if I call the <strong><em>/categoriestree</em></strong> resource first, I get CategoryTreeSerialized serialization.<br/>
  But if after I call the <strong><em>/categories</em></strong> resource is always serialized with the CategoryTreeSerialized class and not with CategorySerialized</p>
  
  <p>(And vice versa)</p>