2016-12-28 58 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