How to add custom deserializer to interface using jackson

后端 未结 2 1838
挽巷
挽巷 2021-01-02 07:42

Saying I have an interface A, I want to use custom deserializer for all classes implement interface A, So I use code below but it doesn\'t work, While CustomAserializer wo

2条回答
  •  余生分开走
    2021-01-02 08:08

    Just in case someone need a solution to serialize and desiralize inheritance hierarchy

    you can use jackson annotation in more elegant way : JsonTypeInfo and JsonSubTypes

     @JsonTypeInfo(
      use = JsonTypeInfo.Id.NAME, 
      include = JsonTypeInfo.As.PROPERTY, 
      property = "type")
    @JsonSubTypes({ 
      @Type(value = ServiceUser.class, name = "service"), 
      @Type(value = AdminUser.class, name = "admin") 
    })
    public interface User{
        // ...
    }
    

提交回复
热议问题