I have a problem related to:
De-serializing JSON to polymorphic object model using Spring and JsonTypeInfo annotation
The solutions provided there didn't work for me. I have the following DTO:
public class QuestionaireAnswersDTO {
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = As.EXISTING_PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(name = "single", value = SingleChoiceAnswerDTO.class),
@JsonSubTypes.Type(name = "multi", value = MultipleChoiceAnswerDTO.class)
})
public static abstract class QuestionaireAnswerDTO {
String answerId;
String name;
public String getAnswerId() {
return answerId;
}
public void setAnswerId(String answerId) {
this.answerId = answerId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
String questionnaireId;
List<QuestionaireAnswerDTO> answers;
public String getQuestionnaireId() {
return questionnaireId;
}
public void setQuestionnaireId(String questionnaireId) {
this.questionnaireId = questionnaireId;
}
public List<QuestionaireAnswerDTO> getAnswers() {
return answers;
}
public void setAnswers(List<QuestionaireAnswerDTO> answers) {
this.answers = answers;
}
with those subclasses:
public static class SingleChoiceAnswerDTO extends QuestionaireAnswerDTO {
@Nullable
String selectedOption;
public String getSelectedOption() {
return selectedOption;
}
public void setSelectedOption(String selectedOption) {
this.selectedOption = selectedOption;
}
}
public static class MultipleChoiceAnswerDTO extends QuestionaireAnswerDTO {
List<String> selectedOptions;
public List<String> getSelectedOptions() {
return selectedOptions;
}
public void setSelectedOptions(List<String> selectedOptions) {
this.selectedOptions = selectedOptions;
}
}
Now I wanted to write a test using this json object:
{
"questionnaireId":"questionnaire1",
"answers":[
{
"name":"single",
"answerId":"Question1",
"selectedOption":"Yes"
},
{
"name":"multi",
"answerId":"Question3",
"selectedOptions":[
"yes",
"no"
]
}
]
}
Using this test:
JsonFactory factory = new JsonFactory();
factory.enable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
ObjectMapper mapper = new ObjectMapper(factory);
mapper.registerSubtypes(QuestionaireAnswersDTO.SingleChoiceAnswerDTO.class, QuestionaireAnswersDTO.MultipleChoiceAnswerDTO.class);
QuestionaireAnswersDTO result = mapper.readValue(testData, QuestionaireAnswersDTO.class);
String resultAsString = mapper.writeValueAsString(result);
System.out.println(resultAsString);
Which results in the following Error:
com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve subtype of [simple type, (...)
missing type id property '@class' (for POJO property 'answers')
Using the .registerSubtypes() method instead of JsonSubtypes didn't work here instead of JsonSubtypes. Same error occurs.