Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing JAXB to parse JSON in Jersey
    primarykey
    data
    text
    <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("/") public class WebService { @GET @Produces(MediaType.TEXT_PLAIN) public String status() { return "Web service is up and running."; } @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 "Latitude: " + request.latitude + "\n:Longitude: " + 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="1.0" encoding="UTF-8"?&gt; &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"&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>{ "latitude":"25.764084501106787", "longitude":"-80.37422332275389" } </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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload