Jackson JSON, Immutable Classes, and Interfaces

前端 未结 1 936
暖寄归人
暖寄归人 2021-02-07 06:52

I am playing with the Jackson examples and am having some trouble getting deserialization to work with immutable classes and interfaces.

Below is my code:



        
相关标签:
1条回答
  • 2021-02-07 07:28

    Just in case you hadn't seen it, here's a blog entry that discusses working with immutable objects and Jackson.

    But you should definitely be able to use @JsonDeserialize(as=AddressImpl.class); either by adding it to Address.java interface (either directly or by using mix-ins), or by adding it to field or property. One thing to note is that for deserialization, it MUST be next to accessor you use; setter if you have one, if not, next to field. Annotations are not (yet) shared between accessors; so for example adding it to 'getter' would not work.

    Jackson 1.8 also finally allows registration of abstract-to-concrete types (see http://jira.codehaus.org/browse/JACKSON-464 for more details) which might be the best option to indicate that 'AddressImpl' is to be used for 'Address'.

    0 讨论(0)
提交回复
热议问题