我有这样的类(class)
public class Profile {
Profile profile
Interests interests
}
public class Interests {
HashMap<String,InterestMetric> interests = ...
}
如果我尝试使用 jackson 将其序列化为 json:
{
profile:...
interests:
{
interests:...
}
}
有没有办法避免在没有自定义序列化器(注释)的情况下创建 2 个兴趣标签?
最佳答案
是的:
public class Profile {
Profile profile;
HashMap<String,InterestMetric> interests = ...
}
如果第二个类只包含 map ,为什么还需要它?
再想一想...为什么 Profile 包含 Profile?它不应该只包含配置文件中汇总的信息吗?
编辑: 既然您提到代码库不会改变, @JsonUnwrapped annotation应该是您正在寻找的:
public class Profile {
Profile profile
@JsonUnwrapped
Interests interests
}
public class Interests {
HashMap<String,InterestMetric> interests = ...
}
我无法从我所在的位置测试它,但它应该将您的输出更改为:
{
profile:...
interests:...
}
关于java - 使用 Jackson json 跳过一层抽象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21380634/