I created a very simple RESTful servise using Jersey following this guide. I adjusted the generated sample code to have a polymorphic structure where Animal is the base class and Sheep and Goat inherit from it:
@XmlRootElement
public abstract class Animal {
public Animal() {}
}
@XmlRootElement
public class Sheep extends Animal {
public String fur;
public Sheep() {this.fur = "curly";}
}
@XmlRootElement
public class Goat extends Animal {
public String color;
public Goat() {this.color = "yellow";}
}
and following resource class which produces the response:
@Path("animal")
public class MyResource {
@GET
@Path("{animal}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Animal getIt(@PathParam("animal") String animal) {
return (animal.equals("sheep")) ? new Sheep() : new Goat();
}
}
the project dependencies in my pom.xml are:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
</dependency>
</dependencies>
When I run the application in Eclipse on Server (using Tomcat v8.0 if that matters) and try to request the XML response, e.g. in a browser, the output is correct like this:
<sheep>
<fur>curly</fur>
</sheep>
or if I request a goat:
<goat>
<color>yellow</color>
</goat>
but once I request the JSON response, the output is an empty JSON document: {} I use curl to get the JSON media type like this:
curl -H "Accept: application/json" http://localhost:8080/simple-service-webapp/api/animal/goat
What do I need to configure or change to get a JSON response representing the correct object? I'm new to this topic, I hope the question is clear. Thank you.
PS: If the return type of the method getIt is set to e.g. Sheep and Sheep object is returned, the JSON response is correct, so I suppose I need to map the inheritance somewhere somehow so it recognizes the returned type, but I can not find out what to do.
Seem this requires the use of @XmlSeeAlso
@XmlRootElement
@XmlSeeAlso({Sheep.class, Goat.class})
public abstract class Animal {
public Animal() {}
}
C:\>curl -H "Accept:application/json" http://localhost:8080/api/animal/sheep
{"type":"sheep","fur":"curly"}