title: Mapping to JSON using Jersey in Jetty
body: <p>I have a Jetty server (8.1.3) where I want to expose some POJOs over a REST service using Jersey (1.12). I have problem with mapping resources to JSON and I can't figure out why.</p>
  
  <p>Jetty and Jersey servlet start:</p>
  
  <pre><code>public class JettyUtil {
    public static void start() throws Exception {
      Server server = new Server();
      SelectChannelConnector connector = new SelectChannelConnector();
      connector.setPort(8999);
      server.addConnector(connector);
  
      HandlerList handlers = new HandlerList();
      handlers.setHandlers(new Handler[] { createJerseyServlet(), new DefaultHandler() });
      server.setHandler(handlers);
  
      server.start();
      server.join();  
    }
  
    private static Handler createJerseyServlet() {
      ServletContextHandler handler = new ServletContextHandler();
      handler.setContextPath("/game");
      SessionHandler sessionHandler = new SessionHandler();
      handler.setSessionHandler(sessionHandler);
      ServletHolder holder = handler.addServlet("com.sun.jersey.spi.container.servlet.ServletContainer", "/*");
      Map&lt;String,String&gt; initParams = ImmutableMap.of(
               "com.sun.jersey.config.property.packages", "com.test.todo.rest",
               "com.sun.jersey.api.json.POJOMappingFeature", "true");
      holder.setInitParameters(initParams);
      return handler;
    }
   }
  </code></pre>
  
  <p>Resource:</p>
  
  <pre><code>@XmlRootElement
  public class Todo {
    private String summary;
    public String getSummary() { return summary; }
    public void setSummary(String summary) { this.summary = summary; }
  }
  </code></pre>
  
  <p>Webservice:</p>
  
  <pre><code>@Path("/todo")
  public class TodoResource {
    @GET
    @Produces( { MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public Todo getTodo() {
      Todo todo = new Todo();
      todo.setSummary("This is my first todo");
      return todo;
    }
   }
  </code></pre>
  
  <p>Test:</p>
  
  <pre><code>ClientConfig config = new DefaultClientConfig();
  Client client = Client.create(config);
  WebResource service = client.resource(getBaseURI());     
  System.out.println(
    service.path("game").path("todo").
      accept(MediaType.APPLICATION_JSON).get(String.class));
  </code></pre>
  
  <p>Output Jetty server:</p>
  
  <pre><code>May 24, 2012 4:21:34 PM com.sun.jersey.spi.container.ContainerResponse write
  SEVERE: A message body writer for Java class com.test.todo.rest.Todo, and Java type class com.test.todo.rest.Todo, and **MIME media type application/json was not found**
  
  May 24, 2012 4:21:34 PM com.sun.jersey.spi.container.ContainerResponse write
  SEVERE: The registered message body writers compatible with the MIME media type are:
  */* -&gt;
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.core.impl.provider.entity.StringProvider
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.ReaderProvider
  com.sun.jersey.core.impl.provider.entity.DocumentProvider
  com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
  com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
  com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
  com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
  
  May 24, 2012 4:21:34 PM com.sun.jersey.spi.container.ContainerResponse logException
  SEVERE: Mapped exception to response: 500 (Internal Server Error)
  javax.ws.rs.WebApplicationException: 
  com.sun.jersey.api.MessageException: A message body writer for Java class com.test.todo.rest.Todo, and Java type class com.test.todo.rest.Todo, and MIME media type application/json was not found
      at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:285)
      at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.jvva:1451)
  ...
  </code></pre>
  
  <p>Question:</p>
  
  <p>How shall I add 'application/json' as a supported MIME format using a Jersey servlet in Jetty? I thought setting com.sun.jersey.api.json.POJOMappingFeature to true was the trick? (Fetching XML works fine, but I need it to work with Json also)</p>