I would like to write a Custom Deserialization component that would still use some of the default capabilities:
Imagine those classes:
class Category {
private Long id;
private String name;
}
class Article {
private Long id;
private String title;
private Category category;
}
And this JSON payload:
{
"id": 1,
"title": "Pray for Notre Dame",
"category_id": 5
}
With my understanding of the various articles I read online, I should write a custom deserializer to handle my category_id
field:
@JsonComponent
public class ArticleJsonDeserializer extends JsonDeserializer<Article> {
@Override
public Article deserialize(JsonParser jsonParser,
DeserializationContext deserializationContext) throws IOException,
JsonProcessingException {
TreeNode treeNode = jsonParser.getCodec().readTree(jsonParser);
TextNode category = this.entityManager.getReference(Category.class, treeNode.get("category_id"));
// The following lines bother me
String title = treeNode.get("title");
Long id = treeNode.get("id");
return new Article(id, title, category);
}
}
However, in reality, I would love to use the default deserializer capabilities for the title
and id
fields without rewriting this logic.
How can I do that?