CodeZLei

  博客园 :: 首页 :: 博问 :: 闪存 :: :: 联系 :: 订阅 订阅 :: 管理 ::

1. 使用抽象类来接收json,实现一个抽象对象用多个实例对象获取

代码

导包

   <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.7</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.7</version>
    </dependency>

 

抽象类

package javastu;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(
        use = JsonTypeInfo.Id.NAME,
        property = "type"
)
@JsonSubTypes({
        @JsonSubTypes.Type(name = "A", value = AStudent.class),
        @JsonSubTypes.Type(name = "B", value = BStudent.class)
})
public abstract class Student {

    private String age;

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }
}

实现类

package javastu;

public class AStudent extends Student{
    private String A;

    public String getA() {
        return A;
    }

    public void setA(String a) {
        A = a;
    }

    @Override
    public String toString() {
        return "AStudent{" +
                "A='" + A + '\'' +
                '}';
    }
}
package javastu;

public class BStudent extends Student{
    private String B;

    public String getB() {
        return B;
    }

    public void setB(String b) {
        B = b;
    }

    @Override
    public String toString() {
        return "BStudent{" +
                "B='" + B + '\'' +
                '}';
    }
}

测试

package javastu;


import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class App {

    public static void main(String[] args) throws IOException {
        AStudent studentA = new AStudent();
        studentA.setA("AA");
        studentA.setAge("111");

        BStudent studentB = new BStudent();
        studentB.setB("BB");
        studentB.setAge("222");

        ObjectMapper objectMapper = new ObjectMapper();

        String stuAStr = objectMapper.writeValueAsString((Student) studentA);
        String stuBStr = objectMapper.writeValueAsString(studentB);

        Student student2A = objectMapper.readValue(stuAStr, Student.class);
        Student student2B = objectMapper.readValue(stuBStr, Student.class);

        System.out.println(stuAStr);
        System.out.println(stuBStr);

        System.out.println(student2A instanceof AStudent);
        System.out.println(student2B instanceof BStudent);


    }
}

结果:

{"type":"A","age":"111","a":"AA"}
{"type":"B","age":"222","b":"BB"}
true
true

 

 其他方式

https://blog.csdn.net/bruce128/article/details/80298808

 ObjectMapper objectMapper = new ObjectMapper()
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
View Code

 

posted on 2019-06-14 16:54  CodeZLei  阅读(611)  评论(0编辑  收藏  举报