title: Using JAXB to parse JSON in Jersey
body: <p>I'm trying to create a very simple RESTful web service using Jersey. I'm trying to make it so that it consumes and produces a JSON by using JAXB. The problem is that I get an error when I pass a JSON to it.</p>
  <p>Below is the resource code. Both status() and echo() are working properly. Please note that on processRequest() I'm currently producing a text response, but that will be changed later to produce a JSON.</p>
  <pre><code>package web_service;
  
  import javax.ws.rs.*;
  import javax.ws.rs.core.*;
  
  @Path(&quot;/&quot;)
  public class WebService {
  
      @GET
      @Produces(MediaType.TEXT_PLAIN)
      public String status() {
          return &quot;Web service is up and running.&quot;;
      }
  
      @POST
      @Consumes(MediaType.TEXT_PLAIN)
      @Produces(MediaType.TEXT_PLAIN)
      public String echo(String consumed) {
          return consumed;
      }
  
      @POST
      @Consumes(MediaType.APPLICATION_JSON)
      @Produces(MediaType.TEXT_PLAIN)
      public String processRequest(JAXBElement&lt;Request&gt; r) {
          Request request = r.getValue();
          return &quot;Latitude: &quot; + request.latitude +
              &quot;\n:Longitude: &quot; + request.longitude;
      }
  }
  </code></pre>
  <p>This is the Request model:</p>
  <pre><code>package web_service;
  
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement
  public class Request {
  
      public String latitude;
      public String longitude;
  
      public Request() {}
  
      public Request(String latitude, String longitude) {
          this.latitude = latitude;
          this.longitude = longitude;
      }
  
      // Getters and setters for both
  }
  </code></pre>
  <p>My web.xml:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; 
      xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot; 
      xmlns:web=&quot;http://java.sun.com/xml/ns/javaee&quot; 
      xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot; 
      id=&quot;WebApp_ID&quot; 
      version=&quot;2.5&quot;&gt;
  
       &lt;servlet&gt;
           &lt;servlet-name&gt;Jersey REST Service&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;web_service&lt;/param-value&gt;
           &lt;/init-param&gt;
  
           &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;Jersey REST Service&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  <p>Finally, this is an example of the POST that I'm doing (headers set to 'Content-Type: application/json');</p>
  <pre><code>{
      &quot;latitude&quot;:&quot;25.764084501106787&quot;,
      &quot;longitude&quot;:&quot;-80.37422332275389&quot;
  }
  </code></pre>
  <p>When I run this, Tomcat gives me the following Exception:</p>
  <pre><code>WARNING: WebApplicationException cause:
  org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=application/json, type=class web_service.Request, genericType=class     web_service.Request.
  </code></pre>
  <p>And I get the following response:</p>
  <pre><code>415 Unsupported Media Type
  
  Content-Length: 1 kB
  Content-Type:   text/html;charset=utf-8
  Date:           2013 Nov 4 17:41:20
  Server:         Apache-Coyote/1.1
  </code></pre>
  <p><img src="https://i.stack.imgur.com/bZkGP.png" alt="enter image description here" /></p>
  <p>I'm very new to this and this is not making a lot of sense. Hopefully, one of you will be able to give me a hand. Thanks!</p>