Note that there are some explanatory texts on larger screens.

plurals
  1. POCan a Jersey GET request return a polymorphic entity?
    primarykey
    data
    text
    <p>I've got a Resource class that attempts to return an interface type, say "Shape":</p> <pre><code>public interface Shape {...} @XmlRootElement public class Circle implements Shape {...} @Path("/api/shapes") public class ShapeResource { @GET @Path("/{shapeId}") public Shape get(@PathParam("shapeId") String shapeId) { .... return new Circle(); } } </code></pre> <p>Experimenting with the above, I see that the server is returning XML like so:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;circle&gt; ... &lt;/circle&gt; </code></pre> <p>So far so good. The problem is, the client doesn't know how to unmarshall this. I'm getting:</p> <pre><code>com.sun.jersey.api.client.ClientHandlerException: A message body for Java type, interface Shape, and MIME media type, application/xml, was not found </code></pre> <p>Given a WebResource, and asking for an entity type of Shape.class causes an exception.</p> <p>The server seems to be doing the correct thing. I've been struggling for hours on how to get the Client to deserialize this class. I even tried wrapping the interface I'm really trying to get in a wrapper as outlined here: <a href="https://jaxb.dev.java.net/guide/Mapping_interfaces.html" rel="noreferrer">https://jaxb.dev.java.net/guide/Mapping_interfaces.html</a>. That didn't work either.</p> <p>My Client code looks like this:</p> <pre><code> ClientResponse response = get(wr, ClientResponse.class); // wr == WebResource try { return response.getEntity(Shape.class); // &lt;-- FAIL } catch (Exception e) { e.printStackTrace(); // com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java type, interface Shape, and MIME media type, application/xml, was not found } try { return response.getEntity(Circle.class); // &lt;-- WIN, but hacky and forces me to try every concrete type } catch (Exception e) {} return null; </code></pre> <p>Any insight or guidance is greatly appreciated. Thanks in advance.</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.
 

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