Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Polymorphic deserialization Json to POJO in a REST module

Oleksii Skachkov
Contributor
March 19, 2020

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.

1 answer

0 votes
Daniel Eads
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
March 20, 2020

Hey Oleksii,

Exciting that you're developing on Jira Server! I must admit my Java is a little rusty (and I don't mean Rust), so I don't have a straightforward answer for you about this.

We do however have a dedicated Developer Community that might be good to check out for plugin development related questions. Since the topics are more focused, it's easier for other developers to watch the questions coming in and provide guidance. I would try asking there, as the general Community here is a little more targeted at general users and administrators. Plus, the topics on the Developer Community are also very interesting to read!

Cheers,
Daniel | Atlassian Support

Oleksii Skachkov
Contributor
March 21, 2020

Thanks for your advice, Daniel. I will ask there.

Like Daniel Eads likes this

Suggest an answer

Log in or Sign up to answer