gpt4 book ai didi

java - 获取详细的 json 响应(没有 id 引用,始终是实例)

转载 作者:行者123 更新时间:2023-11-30 06:14:30 25 4
gpt4 key购买 nike

我有以下对象:

public class A
{
private String id;
private String name;
private List <B> bs;
}

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class B
{
private String id;
private String name;
}

序列化后,如果我使用restcontroller执行get操作,我会得到类似这样的东西,首先是完整的实例,然后是引用:

    {
"A": [
{
"id": "1",
"bs": [{
"id": "1",
"name": "b1"
},
{
"id": "2",
"name": "b2"
}]
},
{
"id": "2",
"bs": ["1","2"]
}]
}

是否有机会始终获得完整的实例而不是引用?例如

{
"A": [
{
"id": "1",
"bs": [{
"id": "1",
"name": "b1"
},
{
"id": "2",
"name": "b2"
}]
},
{
"id": "2",
"bs": [{
"id": "1",
"name": "b1"
},
{
"id": "2",
"name": "b2"
}]
}]
}

最佳答案

由于您在评论中指出您依赖 @JsonIdentityInfo 注释进行反序列化,因此不能删除该注释。

但是,您可以做的是创建一个混合,专门覆盖此特定用例的对象标识行为:

@JsonIdentityInfo(generator = ObjectIdGenerators.None.class)
public abstract class BMixIn {}

然后将其注册到用于编写输出的 ObjectMapper 上:

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixIn(B.class, BMixIn.class);

关于java - 获取详细的 json 响应(没有 id 引用,始终是实例),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49530530/

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