title: Using default Providers/MessageBodyWriters in Jersey 2
body: <p>Just starting with Jersey, I've been trying to reproduce the simple example in the latest Jersey documentation '<a href="https://jersey.java.net/documentation/latest/representations.html#d0e5170" rel="noreferrer">building responses</a>'. This part, as far as I understand, should show how <code>Response</code> and <code>ResponseBuilder</code> can be used to easily return a response in combination with <code>Entity&lt;T&gt;</code> for response content.</p>
  
  <p>Now, the documentation states that several data types are by default supported (here: '<a href="https://jersey.java.net/documentation/latest/representations.html#reps-and-types" rel="noreferrer">Representations and Java types</a>'). <code>String</code> prime among them, matching any media type.</p>
  
  <p>Of all the variations I've tried, the following is the simplest:</p>
  
  <pre><code>@POST
  public Response post() {
      URI createdUri;
      try {
          createdUri = new URI("http://test.lan");
      } catch (final URISyntaxException e) {
          throw new WebApplicationException(e);
      }
  
      return Response.created(createdUri).entity(Entity.text("someContent")).build();
  }
  </code></pre>
  
  <p>I've always gotten the same error (full stacktrace below) on calling the request: 
  <code>org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/plain, type=class javax.ws.rs.client.Entity, genericType=class javax.ws.rs.client.Entity.</code></p>
  
  <p>I believe it's saying that a suitable provider wasn't found for this Entity generic-type. However, String should be supported OOTB?</p>
  
  <p>I found that <code>StringMessageProvider</code> is probably the Jersey 1 implementation of this provider, and the closest related classes I found in my Jersey 2 libraries arer the classes in <code>org.glassfish.jersey.message.internal</code> in jersey-common. Among the many providers there is the <code>StringMessageProvider</code>, which appears to me like a potential intended provider for this.</p>
  
  <p>I've looked up the issue, and while there's plenty of people who get this when incorrectly trying to use a custom Provider, I found nothing about the default OOTB providers not working..</p>
  
  <p>I've checked my libs, and right now I have the following dependencies in my pom (among others):</p>
  
  <ul>
  <li>jersey-container-servlet-core</li>
  <li>jersey-client</li>
  <li>jersey-common</li>
  <li>jersey-server</li>
  </ul>
  
  <p>I've looked online but this seems to be all I need, although I have not with certainty found the right provider classes for String and  JAXB/JSON in the jars.</p>
  
  <h2>Context</h2>
  
  <ul>
  <li>Maven project</li>
  <li>with tomcat servlet-api 6.0.29</li>
  <li>Version 2.6 of all mentioned jersey libs</li>
  <li>Eclipse kepler</li>
  <li>Using tomcat6 maven plugin to run embedded tomcat (works fine so far)</li>
  </ul>
  
  <h2>Fiddler request used to test</h2>
  
  <pre><code>POST HTTP/1.1
  User-Agent: Fiddler
  Host: 127.0.0.1
  Content-Length: 0
  </code></pre>
  
  <p>And again tried several variations.</p>
  
  <h2>Full stacktrace</h2>
  
  <pre><code>06-Jan-2015 21:13:54 org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor aroundWriteTo
  SEVERE: MessageBodyWriter not found for media type=text/plain, type=class javax.ws.rs.client.Entity, genericType=class javax.ws.rs.client.Entity.
  06-Jan-2015 21:13:54 org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet TestService threw exception
  org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=text/plain, type=class javax.ws.rs.client.Entity, genericType=class javax.ws.rs.client.Entity.
      at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:247)
      at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
      at org.glassfish.jersey.server.internal.JsonWithPaddingInterceptor.aroundWriteTo(JsonWithPaddingInterceptor.java:103)
      at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
      at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundWriteTo(MappableExceptionWrapperInterceptor.java:88)
      at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
      at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1154)
      at org.glassfish.jersey.server.ServerRuntime$Responder.writeResponse(ServerRuntime.java:571)
      at org.glassfish.jersey.server.ServerRuntime$Responder.processResponse(ServerRuntime.java:378)
      at org.glassfish.jersey.server.ServerRuntime$Responder.process(ServerRuntime.java:368)
      at org.glassfish.jersey.server.ServerRuntime$1.run(ServerRuntime.java:262)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
      at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
      at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
      at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:319)
      at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:236)
      at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1028)
      at org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:373)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
      at org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:219)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
      at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
      at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
      at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
      at java.lang.Thread.run(Thread.java:662)
  </code></pre>
  
  <h2>EDIT</h2>
  
  <p>The same error (for application/json) ocurs now that I've annotated a class with <code>@XmlRootElement</code> and try to return it in a method per the Jersey docs:</p>
  
  <pre><code>@GET
  @Produces(MediaType.APPLICATION_JSON) 
  public Foo sampleFoo() {
      Foo foo = new Foo();
  
      return foo;
  }
  </code></pre>
  
  <p>Where <code>Foo</code> is annotated with <code>@XmlRootElement</code>.</p>
  
  <p>I also added jersey-media-json-jackson as dependency, which I can see contains an explicit JSONJaxb provider. However, it doesn't seem to be picked up somehow.</p>