0

I have a simple situation. I have a main DTO class with the following fields: AnimalDTO

public class AnimalDTO {
    @JsonCreator
    public AnimalDTODTO(@JsonProperty("error") boolean error,
                                       @JsonProperty("errorMessage") String errorMessage,
                                       @JsonProperty("cat") CatDTO cat,
                                       @JsonProperty("dog") DogDTO dog) {
        this.error = error;
        this.errorMessage = errorMessage;
        this.cat = cat;
        this.dog = dog;
    }

    private boolean error;
    private String errorMessage;
    private CatDTO cat;
    private DogDTO dog;
}

CatDTO:

public class CatDTO {
    @JsonCreator
    public CatDTO(@JsonProperty("catNumber") String catNumber,
                  @JsonProperty("catState") String catState) {
        this.catNumber = catNumber;
        this.catState = catState;
    }

    private String catNumber;
    private String catState;
}

DogDTO:

public class DogDTO {
    @JsonCreator
    public DogDTO(@JsonProperty("dogNumber") String dogNumber,
                  @JsonProperty("dogState") String dogState
                  @JsonProperty("dogColor") String dogColor
                  @JsonProperty("dogBalance") BigDecimal dogBalance) {
        this.dogNumber = dogNumber;
        this.dogState = dogState;
        this.dogColor = dogColor;
        this.dogBalance = dogBalance;
    }

    private String dogNumber;
    private String dogState;
    private String dogColor;
    private BigDecimal dogBalance;

}

and from external API I have responses (and I can't change it) for dog like:

{
   "dogNumber": "23",
   "dogState": "good",
   "dogColor": "pink",
   "dogBalance": 5
}

and for Cat:

{
   "catNumber": "1",
   "catState": "good"
}

And I want to use Jackson mapper like this: objectMapper.readValue(stringJson, AnimalDTO.class);

I was thinking to add in AnimalDTO:

@JsonTypeInfo(
  use = JsonTypeInfo.Id.NAME, 
  include = JsonTypeInfo.As.PROPERTY, 
  property = "type")
@JsonSubTypes({ 
  @Type(value = CatDTO.class, name = "cat"), 
  @Type(value = DogDTO.class, name = "dog") 
})

but it's not working.

How to handle my case in best way?

FPerez
  • 1
  • 2
  • You showed us the cat json and the dog json...do you have a an animal json? Or are these 2 JSONS completely independent?? – JCompetence Nov 25 '21 at 08:14
  • @SMA - No, I dont have an animal json...these 2 JSONS completely independent – FPerez Nov 25 '21 at 08:22
  • then if I am not mistaken, you need to create a DTO for each and you can't use `JsonTypeInfo`. Based on my understand `JsonTypeInfo` and `JsonSubTypes` are used when you have inheritance in your JSONs. In your case each JSON has completely different variables.... – JCompetence Nov 25 '21 at 08:26
  • Since two classes are independent (no inheritance hierachy) you don't need `@JsonTypeInfo` nor `@JsonSubTypes`. You can directly serialize/deserialize **animal json** to `AnimalDTO` which will have `CatDTO` and `DogDTO` respectively. – Ruwanka De Silva Nov 25 '21 at 10:23

1 Answers1

0

Your code will work just fine (without any @JsonTypeInfo or @JsonSubTypes annotations) if the JSON that you get from the external API is as follows:

{
  "dog": {
    "dogNumber": "23",
    "dogState": "good",
    "dogColor": "pink",
    "dogBalance": 5
  },
  "cat": {
    "catNumber": "1",
    "catState": "good"
  }
}

If this does not look similar to the JSON you receive then you need to add it to your answer so that we can help you further.

João Dias
  • 16,277
  • 6
  • 33
  • 45