I'm using Spring Boot (latest version).
I have this 4 classes. SubtypeDTO inherits from abstract class TypeDTO, ClassUsingSubtype uses SubtypeDTO and ClassUsingType uses TypeDTO. I need all of them!
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME)
@JsonSubTypes({@JsonSubTypes.Type(value = SubtypeDTO.class)})
public abstract class TypeDTO {
}
public class SubtypeDTO extends TypeDTO {
private String field;
...constructor, getter, setter...
}
public class ClassUsingSubtype {
private SubtypeDTO dto;
...constructor, getter, setter...
}
public class ClassUsingType {
private TypeDTO dto;
...constructor, getter, setter...
}
I have this endpoint:
@PostMapping("classWithSubtype")
public void classWithSubtype(@RequestBody ClassUsingSubtype dto) {
...
}
My problem is that this ClassUsingSubtype JSON won't work (error 400):
{
"dto": {
"field": "value"
}
}
I need to include the @type, despite of the fact that ClassUsingSubtype is using the concrete class SubtypeDTO:
{
"dto": {
"@type": "SubtypeDTO",
"field": "value"
}
}
Clearly, in this case the type is not necessary since Jackson know exactly what to expect (a SubtypeDTO). I want to create a JSON of ClassUsingSubtype without the @type.
Any advice? Am I missing some configuration in my files?