I have a Resource class that is trying to return an interface type, like "Shape":
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(); } }
Experimenting with the above, I see that the server returns XML as follows:
<?xml version="1.0" encoding="UTF-8"?> <circle> ... </circle>
So far so good. The problem is that the client does not know how to undo this. I get:
com.sun.jersey.api.client.ClientHandlerException: A message body for Java type, interface Shape, and MIME media type, application/xml, was not found
Given a WebResource and requesting an entity type of Shape.class, an exception is thrown.
The server seems to be doing the right thing. I struggled for many hours on how to get the Client to deserialize this class. I even tried wrapping the interface that I really try to get in the shell as described here: https://jaxb.dev.java.net/guide/Mapping_interfaces.html . That didn't work either.
My client code is as follows:
ClientResponse response = get(wr, ClientResponse.class); // wr == WebResource try { return response.getEntity(Shape.class); // <-- 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); // <-- WIN, but hacky and forces me to try every concrete type } catch (Exception e) {} return null;
Any insight or guidance is appreciated. Thanks in advance.
java rest jersey jax-rs
Erik
source share