0

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?

2
  • What is the Jackson version? Internet says that need to upgrade it to 2.15 for solving issue Commented Jul 13 at 10:36
  • @Max It's 2.18 version Commented Jul 13 at 22:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.