I have a POJO class which will be deserialized from JSON which comes as RequestBody
in Spring. This POJO class contains List<String>
as an instance variable. When this variable is deserialized, value is null
. How to correctly deserialize it? Below are the code samples
My POJO looks like below
@Getter
@AllArgsConstructor
@NoArgsConstructor
public class BookDto {
@JsonProperty("bookName")
private String bookName;
@JsonProperty("authors")
private List<String> authors;
@JsonProperty("description")
private String description;
}
My JSON object looks like below
{
"bookName":"Vagrant Up and Running",
"authors":["aone", "atwo"],
"description":"Getting started guide for Vagrant development"
}
Below is the sample method call
public void method(@RequestBody BookDto bookDto) {}
I had followed given suggestion and the deserializer code is given below
public class AuthorDeserializer extends JsonDeserializer<List<String>> {
@Override
public List<String> deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
Author author = jsonParser.readValueAs(Author.class);
return author.authors;
}
private static class Author {
public List<String> authors;
}
}