How to de/serialize an immutable object without default constructor using ObjectMapper?

前端 未结 3 1208
悲哀的现实
悲哀的现实 2020-12-04 13:39

I want to serialize and deserialize an immutable object using com.fasterxml.jackson.databind.ObjectMapper.

The immutable class looks like this (just 3 internal attr

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 14:26

    You can use a private default constructor, Jackson will then fill the fields via reflection even if they are private final.

    EDIT: And use a protected/package-protected default constructor for parent classes if you have inheritance.

提交回复
热议问题