-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Description
Hi,
I have an abstract super class annotated with
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
where type
is an existing property of the class.
The full class hierarchy looks like this:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
@JsonSubTypes.Type(value = SubClassA.class, name = "a"),
@JsonSubTypes.Type(value = SubClassB.class, name = "b")
})
public abstract class AbstractSuperClass {
@Transient
private String type;
protected AbstractSuperClass() {
setType(getTypeValue());
}
protected abstract String getTypeValue();
// getters and setters
}
public class SubClassA extends AbstractSuperClass {
private double a;
public SubClassA(double a) {
this.a = a;
}
// getters and setters
@Override
protected String getTypeValue() {
return "a";
}
}
public class SubClassB extends AbstractSuperClass {
private int b;
public SubClassB(int b) {
this.b = b;
}
// getters and setters
@Override
protected String getTypeValue() {
return "b";
}
}
Now when a SubClass of this class gets serialised, the type
is added twice to the generated JSON. E.g. the JSON for a SubClassB would look like this:
{"type":"b","type":"b","b":42}
For the most part this does not seem to be an issue, but some clients (e.g. Postman) refuse this as Malformed JSON
.
I know I could add e.g. @JsonTypeId
or @JsonIgnore
but in that case, type
will not be included in generated JSON when using a List<AbstractSuperClass>
, which is what I definitely need.
I assume that include = JsonTypeInfo.As.EXISTING_PROPERTY
would solve my problem but this results in java.lang.IllegalStateException: Do not know how to construct standard type serializer for inclusion type: EXISTING_PROPERTY