gpt4 book ai didi

java - RESTEasy/Jackson 未将分层 POJO 正确序列化为 JSON

转载 作者:行者123 更新时间:2023-12-01 09:05:01 25 4
gpt4 key购买 nike

我有两个 POJO 定义如下,

public class VertexDefinition {
private final String name;
private final Vertex vertex;

public VertexDefinition(String name, Vertex vertex) {
this.name = name;
this.vertex = vertex;
}

@JsonProperty("name")
public String getName() {
return name;
}

@JsonProperty("properties")
public Iterable<PropertyDefinition> getProperties() {
if(vertex == null) {
return Collections.emptySet();
}
return Iterables.transform(vertex.getPropertyKeys(), new Function<String, PropertyDefinition>() {
@Nullable @Override public PropertyDefinition apply(@Nullable String s) {
return new PropertyDefinition(vertex, s);
}
});
}

@JsonProperty("propertyKeys")
public Iterable<String> getPropertyKeys() {
if (vertex == null) {
return Collections.emptySet();
}
return vertex.getPropertyKeys();
}

}

public class PropertyDefinition {

private final Vertex vertex;
private final String propertyName;

public PropertyDefinition(Vertex vertex, String propertyName) {
this.vertex = vertex;
this.propertyName = propertyName;
}

@JsonProperty("name")
public String getName() {
return propertyName;
}

@JsonProperty("type")
public String getType() {
final Object property = vertex.getProperty(propertyName);

if (property != null) {
return property.getClass().getTypeName();
}

return "(unknown)";
}
}

我的休息方法如下所示,

public Iterable<VertexDefinition> getSchema() {
.....
}

当我发出请求时,我收到如下 json 响应,

 [
{
"name" : "Foo",
"properties" : [],
"propertyKeys" : [
"a",
"b",
"c"
]
},
{
"name" : "Bar",
"properties" : [],
"propertyKeys" : [
"a",
"b",
"c"
]
}
]

简而言之,当填写 propertyKeys 时,我得到一个属性返回的空数组。

我做错了什么?

最佳答案

我认为反序列化为可迭代的效果并不像您尝试过的那样。您可以在 getProperties 方法中尝试类似的方法吗?

List<PropertyDefinition> propertyDefinitions = Arrays.asList(mapper.readValue(json, PropertyDefinition[].class))

关于java - RESTEasy/Jackson 未将分层 POJO 正确序列化为 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41353799/

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