I want to get an object as a POST request parameter. I got an abstract superclass called Promotion and subclasses of Product and Percent . This is how I try to get the request:
@POST @Consumes(MediaType.APPLICATION_XML) @Produces(MediaType.APPLICATION_XML) @Path("promotion/") public Promotion createPromotion(Promotion promotion) { Product p = (Product) promotion; System.out.println(p.getPriceAfter()); return promotion; }
and here, as I use JAXB in class definitions:
@XmlRootElement(name="promotion") @XmlSeeAlso({Product.class,Percent.class}) public abstract class Promotion {
So now the problem is sending a POST request with a body like this:
<promotion> <priceBefore>34.5</priceBefore> <marked>false</marked> <distance>44</distance> </promotion>
and I'm trying to pass it to Product (since in this case the fields "marked" and "distance" belong to the Promotion class, and "priceBefore" refers to the Product class). I get an exception:
java.lang.ClassCastException: Percent cannot be cast to Product.
Percent seems to be selected as a subclass of "default". Why is this and how can I get an object that is Product ?
source share