欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中Jackson的多態(tài)反序列化詳解

 更新時間:2023年11月06日 10:32:59   作者:楊某人信了你的邪  
這篇文章主要介紹了Java中Jackson的多態(tài)反序列化詳解,多態(tài)序列化與反序列化,主要是借助于Jackson的@JsonTypeInfo與@JsonSubTypes注解實(shí)現(xiàn),下面將通過幾個例子來簡述其運(yùn)用,需要的朋友可以參考下

Jackson多態(tài)反序列化

多態(tài)序列化與反序列化,主要是借助于Jackson的@JsonTypeInfo與@JsonSubTypes注解實(shí)現(xiàn),下面將通過幾個例子來簡述其運(yùn)用。

首先,創(chuàng)建幾個基本的實(shí)體類。這些類都比較簡單,有共同的屬性也有不同的屬性,這里為了簡單,共同屬性就只有一個type。

@Data
public class Person {
    protected Integer type;
}
@EqualsAndHashCode(callSuper = true)
@Data
public class Student extends Person {
    private String studentName;
}
@EqualsAndHashCode(callSuper = true)
@Data
public class Teacher extends Person {
    private String teacherName;
}
@EqualsAndHashCode(callSuper = true)
@Data
public class Doctor extends Person{
    private String doctorName;
}

1、通過類型判斷

在父類上加如下注解。因?yàn)槭峭ㄟ^類型進(jìn)行序列化,所以只需要加一個注解就夠了。

@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS)
@Data
public class Person {
    protected Integer type;
}

測試

@Test
void test1() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    Student student = new Student();
    student.setType(0);
    student.setStudentName("三好學(xué)生~");
    System.out.println(objectMapper.writeValueAsString(student));
    String json1 = "{\"@class\":\"com.example.jackson.Student\",\"studentName\":\"三好學(xué)生~\",\"type\":null}";
    String json2 = "{\"@class\":\"com.example.jackson.Teacher\",\"teacherName\":\"十佳教師~\",\"type\":null}";
    System.out.println(objectMapper.readValue(json1, Person.class));
    System.out.println(objectMapper.readValue(json2, Person.class));
}

輸出

{"@class":"com.example.jackson.Student","type":0,"studentName":"三好學(xué)生~"}
Student(studentName=三好學(xué)生~)
Teacher(teacherName=十佳教師~)

如果不想用@class做為類型標(biāo)識,也可使用簡略模式

@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS)
@Data
public class Person {
    protected Integer type;
}

測試

@Test
void test2() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    Student student = new Student();
    student.setType(0);
    student.setStudentName("三好學(xué)生~");
    System.out.println(objectMapper.writeValueAsString(student));
    String json1 = "{\"@c\":\".Student\",\"studentName\":\"三好學(xué)生~\",\"type\":null}";
    String json2 = "{\"@c\":\".Teacher\",\"teacherName\":\"十佳教師~\",\"type\":null}";
    System.out.println(objectMapper.readValue(json1, Person.class));
    System.out.println(objectMapper.readValue(json2, Person.class));
}

輸出

{"@c":".Student","type":0,"studentName":"三好學(xué)生~"}
Student(studentName=三好學(xué)生~)
Teacher(teacherName=十佳教師~)

2、通過屬性值判斷

上面的實(shí)體類中,存在共同屬性,比如type就是一個共同屬性,可以通過這個共同屬性的值來判斷需要反序列化為哪一個類。

如下,當(dāng)選擇屬性為type,當(dāng)值為1時反序列化為Student,值為2時反序列化為Teacher,值為3或4則反序列化為Doctor。

因?yàn)閠ype是類中已存在的屬性,所以@JsonTypeInfo注解中的include屬性設(shè)置為EXISTING_PROPERTY,否則序列化的時候不管原先類中有沒有type屬性,都會在序列化后的json中添加一個type,出現(xiàn)一個json中有兩個相同屬性的情況,這里就不貼出來了,感興趣可以自己去試一下。

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY,
        property = "type", visible = true)
@JsonSubTypes(value = {
        @JsonSubTypes.Type(value = Student.class, name = "1"),
        @JsonSubTypes.Type(value = Teacher.class, name = "2"),
        @JsonSubTypes.Type(value = Doctor.class, names = {"3", "4"})
})
@Data
public class Person {
    protected Integer type;
}

測試

@Test
void test3() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();
    Student student = new Student();
    student.setType(1);
    student.setStudentName("三好學(xué)生~");
    System.out.println(objectMapper.writeValueAsString(student));
    String json1 = "{\"studentName\":\"三好學(xué)生~\",\"type\":1}";
    String json2 = "{\"teacherName\":\"十佳教師~\",\"type\":2}";
    String json3 = "{\"doctorName\":\"華佗在世~\",\"type\":4}";
    System.out.println(objectMapper.readValue(json1, Person.class));
    System.out.println(objectMapper.readValue(json2, Person.class));
    System.out.println(objectMapper.readValue(json3, Person.class));
}

輸出

{"type":1,"studentName":"三好學(xué)生~"}
Student(studentName=三好學(xué)生~)
Teacher(teacherName=十佳教師~)
Doctor(doctorName=華佗在世~)

只不過使用這種方法有弊端,首先就是需要將涉及到反序列化的所有子類都標(biāo)注到基類上便于基類感知,如果子類多了,那就顯得臃腫,而且也違反了開閉原則。所以介紹下面的另一種方式

通過屬性值判斷,除了上面的使用@JsonSubTypes在基類上全都列出來之外,還可以使用@JsonTypeName注解標(biāo)注在子類上,如下

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY,
        property = "type", visible = true)
@Data
public class Person {
    protected Integer type;
}
@EqualsAndHashCode(callSuper = true)
@Data
@JsonTypeName("1")
public class Student extends Person {
    private String studentName;
}

其他幾個類也是一樣,就不貼出來了。

測試

@Test
void test4() throws JsonProcessingException {
    ObjectMapper objectMapper = new ObjectMapper();

    // 關(guān)鍵的是這行
    objectMapper.registerSubtypes(Student.class, Teacher.class, Doctor.class);

    Student student = new Student();
    student.setType(1);
    student.setStudentName("三好學(xué)生~");
    System.out.println(objectMapper.writeValueAsString(student));
    String json1 = "{\"studentName\":\"三好學(xué)生~\",\"type\":1}";
    String json2 = "{\"teacherName\":\"十佳教師~\",\"type\":2}";
    String json3 = "{\"doctorName\":\"華佗在世~\",\"type\":3}";
    System.out.println(objectMapper.readValue(json1, Person.class));
    System.out.println(objectMapper.readValue(json2, Person.class));
    System.out.println(objectMapper.readValue(json3, Person.class));
}

輸出

Student(studentName=三好學(xué)生~)
Teacher(teacherName=十佳教師~)
Doctor(doctorName=華佗在世~)

通過ObjectMapper的registerSubtypes方法注冊子類,這樣一來就不需要在基類上標(biāo)注@JsonSubTypes注解了。

當(dāng)然,除了使用@JsonTypeName注解,然后直接注冊這個類之外,還可以通過下面的方式進(jìn)行注冊,效果是一樣的

objectMapper.registerSubtypes(new NamedType(Student.class, "1"));

上面的代碼是演示,所以在注冊時是一個個寫死的,但實(shí)際上,不可能將所有類全都寫出來進(jìn)行注冊,實(shí)際上可以借助一些工具進(jìn)行來獲取所有的子類,比如Reflections

<dependency>
    <groupId>org.reflections</groupId>
    <artifactId>reflections</artifactId>
    <version>0.10.2</version>
</dependency>
// 獲取該路徑下所有類
Reflections reflections = new Reflections("com.example");
// 獲取對應(yīng)類的所有子類
Set<Class<? extends Person>> classSet = reflections.getSubTypesOf(Person.class);
for (Class<? extends Person> clazz : classSet) {
    // 將帶有@JsonTypeName注解的類進(jìn)行注冊
    if (clazz.isAnnotationPresent(JsonTypeName.class)) {
        objectMapper.registerSubtypes(clazz);
    }
}

到此這篇關(guān)于Java中Jackson的多態(tài)反序列化詳解的文章就介紹到這了,更多相關(guān)Jackson多態(tài)反序列化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 簡單介紹一下什么是microservice微服務(wù)

    簡單介紹一下什么是microservice微服務(wù)

    這篇文章主要介紹了一下什么是microservice微服務(wù)微服務(wù)的定義,微服務(wù)到底是什么意思?什么樣的架構(gòu)可以叫做微服務(wù)?這篇文章可以給你答案
    2023-03-03
  • java代碼塊之簡易qq登錄界面及按鈕顏色設(shè)置代碼

    java代碼塊之簡易qq登錄界面及按鈕顏色設(shè)置代碼

    這篇文章主要介紹了java代碼塊之簡易qq登錄界面及按鈕顏色設(shè)置代碼,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • 基于javassist進(jìn)行動態(tài)編程過程解析

    基于javassist進(jìn)行動態(tài)編程過程解析

    這篇文章主要介紹了基于javassist進(jìn)行動態(tài)編程過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-05-05
  • 在Spring Boot中加載初始化數(shù)據(jù)的實(shí)現(xiàn)

    在Spring Boot中加載初始化數(shù)據(jù)的實(shí)現(xiàn)

    這篇文章主要介紹了在Spring Boot中加載初始化數(shù)據(jù)的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02
  • Java實(shí)現(xiàn)將Markdown轉(zhuǎn)換為純文本

    Java實(shí)現(xiàn)將Markdown轉(zhuǎn)換為純文本

    這篇文章主要為大家詳細(xì)介紹了兩種在 Java 中實(shí)現(xiàn) Markdown 轉(zhuǎn)純文本的主流方法,文中的示例代碼講解詳細(xì),大家可以根據(jù)需求選擇適合的方案
    2025-03-03
  • 將java項(xiàng)目打包成exe可執(zhí)行文件的完整步驟

    將java項(xiàng)目打包成exe可執(zhí)行文件的完整步驟

    最近項(xiàng)目要求,需要將java項(xiàng)目生成exe文件,下面這篇文章主要給大家介紹了關(guān)于如何將java項(xiàng)目打包成exe可執(zhí)行文件的相關(guān)資料,文章通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • java語法糖之jdk迭代的新特性匯總

    java語法糖之jdk迭代的新特性匯總

    什么是語法糖?泛型、自動裝箱拆箱、變長參數(shù)、增強(qiáng)for循環(huán)、switch字符類型、lambda表達(dá)式等,這些其實(shí)都是語法糖。這篇文章主要給大家介紹了關(guān)于java語法糖之jdk迭代的新特性的相關(guān)資料,需要的朋友可以參考下
    2021-05-05
  • java基礎(chǔ)詳解之?dāng)?shù)據(jù)類型知識點(diǎn)總結(jié)

    java基礎(chǔ)詳解之?dāng)?shù)據(jù)類型知識點(diǎn)總結(jié)

    這篇文章主要介紹了java基礎(chǔ)詳解之?dāng)?shù)據(jù)類型知識點(diǎn)總結(jié),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java基礎(chǔ)的小伙伴們有很大的幫助,需要的朋友可以參考下
    2021-04-04
  • 全面了解servlet中cookie的使用方法

    全面了解servlet中cookie的使用方法

    下面小編就為大家?guī)硪黄媪私鈙ervlet中cookie的使用方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • java創(chuàng)建二維碼并賦予url鏈接的功能實(shí)現(xiàn)

    java創(chuàng)建二維碼并賦予url鏈接的功能實(shí)現(xiàn)

    這篇文章給大家分享java創(chuàng)建二維碼并賦予url鏈接的功能實(shí)現(xiàn),需要獲取要賦值給二維碼的鏈接后綴,通過設(shè)置二維碼的訪問路徑等一系列操作,具體實(shí)現(xiàn)代碼跟隨小編一起看看吧
    2021-06-06

最新評論