Hello. I am using Jira Server and have some trouble with deserialization.
I understand, that I need something like this https://www.baeldung.com/jackson-inheritance , but when I try it, a catch exception: "problem: abstract types can only be instantiated with additional type information".
I have an abstract class Vehicle:
import org.codehaus.jackson.annotate.JsonSubTypes;
import org.codehaus.jackson.annotate.JsonSubTypes.Type;
import org.codehaus.jackson.annotate.JsonTypeInfo;
import javax.xml.bind.annotation.XmlRootElement;
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type")
@JsonSubTypes({
@Type(value = Car.class, name = "car"),
@Type(value = Truck.class, name = "truck")
})
@XmlRootElement
public abstract class Vehicle {}
Class Car:
@XmlRootElement
public class Car extends Vehicle {
@XmlElement
private String model;
public Car(String model) {
this.model = model;
}
// no-arg constructor, getters and setters
}
And class Truck:
@XmlRootElement
public class Truck extends Vehicle {
@XmlElement
private String price;
public Truck(String price) {
this.model = model;
}
// no-arg constructor, getters and setters
}
My Rest class has a method:
@POST
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON)
public Response test(Vehicle vehicle) {
Vehicle vehicleTest = vehicle;
return Response.ok(vehicle).build();
}
And I sending ajax from front-part (jQuery) :
test = function(){
var vehicle= {
type: "car",
model: "BMW"
};
return $.ajax({
type: "POST",
data: JSON.stringify(vehicle),
url: instance.path,
contentType: "application/json; charset=utf-8",
dataType: 'json',
mimeType: 'application/json'
})
};I expected to receive Car in my rest module, but catch the exception: "problem: abstract types can only be instantiated with additional type information". I assume that either the annotations were used incorrectly or the JSON was incorrectly compiled. Either the error is small, or the Jira uses a completely different approach.
The request to prompt - in what way it is possible to solve the existing problem.
Thanks.