2016-12-28 54 views
0

我有两个的POJO定义如下,层次的POJO没有被的RESTEasy /杰克逊正确序列化JSON

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被填充。

我在做什么错?

回答

1

我不认为反序列化到一个可迭代的作品你已经试过。你可以尝试这样的事情,而不是在你的getProperties方法?

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

我实际上知道这个工程,但我很好奇为什么Iterable不适用于我的自定义类型,而它对String很好。 –

+1

我认为你可以返回一个迭代,但目前你没有使用任何杰克逊映射器来反序列化你的对象,这是主要问题。看看[这个链接](http://programmerbruce.blogspot.com.au/2011/05/deserialize-json-with-jackson-into.html),我想它可能有你要找的东西 – Dana