De-serializing JSON to polymorphic object model using Spring and JsonTypeInfo annotation

后端 未结 6 1350
[愿得一人]
[愿得一人] 2020-12-29 20:17

I have the following object model in my Spring MVC (v3.2.0.RELEASE) web application:

public class Order {
  private Payment payment;
}

@JsonTypeInfo(use = J         


        
6条回答
  •  天涯浪人
    2020-12-29 20:47

    Rashmin's answer worked, and I found an alternative way to avoid the com.fasterxml.jackson.databind.JsonMappingException: Could not resolve type id into a subtype of Blah issue without needing to use registerSubtypes. What you can do is add the following annotation to the parent class:

    @JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY, property = "type")
    

    Note that the difference is JsonTypeInfo.Id.CLASS instead of JsonTypeInfo.Id.NAME. The downside is that the created JSON will contain the entire class name including the full namespace. The upside is that you don't have to worry about registering subtypes.

提交回复
热议问题