gpt4 book ai didi

java - 使用 Jackson 序列化时合并两个类属性

转载 作者:太空宇宙 更新时间:2023-11-04 09:06:55 26 4
gpt4 key购买 nike

考虑这些类:

public class A {
private String a;
private B b;
}

public class B {
private String b1;
private String b2;
}

有没有一种方法可以使用 Jackson 序列化 A 的对象,而不使用自定义序列化程序,以便结果为:

{"a":"aaa", "b1":"bbb1", "b2":"bbb2"}

最佳答案

您可以简单地使用 @JsonUnwrapped现场

Annotation used to indicate that a property should be serialized "unwrapped"; that is, if it would be serialized as JSON Object, its properties are instead included as properties of its containing Object.

public class A {

private String a;

@JsonUnwrapped
private B b;
}

public class B {

private String b1;

private String b2;

}

关于java - 使用 Jackson 序列化时合并两个类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60127489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com