Reputation: 1032
Here is my class hierarchy:
Banana class:
@JsonTypeName("banana")
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "@type")
public class Banana<T, M> extends Fruit<T, M> {
Fruit class:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes({ @JsonSubTypes.Type(value = Banana.class, name = "banana")})
public class Fruit<T, M> {
private boolean ok;
private T main;
Car class:
@JsonTypeName("car")
public class Car extends Vehicle {
Abstract Vehicle class:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @JsonSubTypes.Type(value = Car.class), @JsonSubTypes.Type(value = AnyOtherClass.class) })
public abstract class Vehicle<K> {
private Date date;
private Id<?> id;
So I create new object:
Banana<Car, String> ba = new Banana<Car, String>();
Car car = new Car("test");
ba.setMain(car);
Banana object has "@type" property.
The Car object has "type" property and if I serialize car as JSON it prints out:
{"type":"car"}
However, if I serialize banana as JSON it prints(just "type":"car" is missing, other object properties are available):
{}
If I do banana.getMain() it prints out
{"type":"car"}
How it is possible?
I tried a simple object(not Car) without any annotation and it works fine as it prints
{"type":"car"}
Have someone any ideas thats going on?
Upvotes: 1
Views: 1706
Reputation: 1032
So the answer is to specify root type for JacksonMapper as
jacksonMapper.writerWithType(new TypeReference<Banana<Car, String>>() {})
Upvotes: 2
Reputation: 116472
You will need to provide a more complete example of usage here. I suspect this is due to type parameterization (that is, trying to serialize instance of generic type) and Java Type Erasure, but it is impossible to say for sure without seeing code you are using.
Upvotes: 1