简体   繁体   English

Spring Boot Jackson Databind-配置继承

[英]Spring Boot Jackson Databind - Configure Inheritance

Suppose I have a base class A : 假设我有一个基类A

public class A {
    public String a;
}

And two child classes B and C : 还有两个子类BC

public class B extends A {
    public String b;
}

public class C extends A {
    public String c;
}

And wrapper of A class: A类的包装:

public class Wrapper {
    public A a;
}

And I have Rest controller that receives client requests as wrapper object: 我有Rest控制器,它接收客户端请求作为包装对象:

@RestController
public class SomeController {

    public void foo(@RequestBody Wrapper wrapper) {}

}

The problem is that Jackson casts wrapper field to base class A . 问题是杰克逊将包装器字段转换为基类A

How can I configure it to recieve correct type? 如何配置它以接收正确的类型?

Annotate your base class A with type information that tells Jackson how to decide whether a given json object should be deserialized to B.java or C.java . 用类型信息注释基类A ,该信息告诉Jackson如何决定将给定的json对象反序列化为B.javaC.java

Ex: With the below code, we are telling jackson that json object for A.class will contain a property with key type whose value can be either "b" or "c". 例如:使用下面的代码,我们告诉杰克逊,A.class的json对象将包含一个键type为属性的属性,其值可以是“ b”或“ c”。 If the value is "b", deserialize the object to B.class , else deserialize it to C.class 如果值为“ b”,则将该对象反序列化为B.class ,否则将其反序列C.class

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")  
@JsonSubTypes({  
    @Type(value = B.class, name = "b"),  
    @Type(value = B.class, name= "c")
    })  
class A {
}

Following is the json that you should be using. 以下是您应该使用的json。

{
   "a" : { // This will be deserialized to B.class
      "type": "b",
      // field of B.class
   }
}



{
   "a" : { // This will be deserialized to C.class
      "type": "c",
      // field of C.class
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Spring Boot 配置 Jackson 转换器? - How to configure Jackson converter with Spring Boot? 如何在 Spring 引导中为 Camel 配置 Jackson ObjectMapper - How to configure Jackson ObjectMapper for Camel in Spring Boot Spring Boot应用程序异常java.lang.NoSuchMethodError:com.fasterxml.jackson.databind.ObjectWriter.forType - Spring boot application exception java.lang.NoSuchMethodError: com.fasterxml.jackson.databind.ObjectWriter.forType com.fasterxml.jackson.databind.JsonMappingException:由于Spring Boot应用程序中的输入结束,没有要映射的内容 - com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input in Spring Boot App Spring Boot/@JDBCTest - 没有可用的“com.fasterxml.jackson.databind.ObjectMapper”类型的合格 bean - Spring Boot/@JDBCTest - No qualifying bean of type 'com.fasterxml.jackson.databind.ObjectMapper' available 如何使用Spring Boot为嵌套实体配置Jackson解串器 - How to configure Jackson deserializer for nested entites with Spring Boot 如何在Spring Boot 1.5.9中全局配置杰克逊非空 - How to configure jackson non null globally in Spring Boot 1.5.9 spring-core的杰克逊数据绑定问题 - jackson-databind issue with spring-core 在春季启动时反序列化杰克逊 - Deserialize jackson in spring boot 春季靴子杰克逊与日期 - Spring boot Jackson with Date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM