2014-10-01 110 views
4

问题是我想隐藏来自RESTFul JSON响应的null元素(如果可能的话)。 REST控制器从Mongo数据库中检索信息,并且因为这些元素不存在,我想在它们为null时忽略它们。隐藏泽西岛的JSON字段RESTful

这是我的休息控制器(暴露​​与泽西):

@Stateless 
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED) 
@Path(PropertiesRestURIConstants.PROPERTIES) 
@Produces(MediaType.APPLICATION_JSON) 
@RequestScoped 
public class GetPropertiesController { 

    @EJB(mappedName = PropertiesManagerRemote.MAPPED_NAME) 
    PropertiesManagerRemote propertiesManager; 

    @GET 
    @Path(PropertiesRestURIConstants.PROPERTIES_ALL) 
    public List<PropertyEntity> getAllProperties() throws DBLayerException { 
     return propertiesManager.getAllProperties(); 
    } 

    ... 
    ... 
    ... 
} 

这是我的实体:

@Document(collection = "property") 
public class PropertyEntity implements GenericEntity { 

    @Id 
    private String id; 

    private String propertyName; 
    private String propertyValue; 

    public PropertyEntity() { 
    } 

    public PropertyEntity(String propertyName, String propertyValue) { 
     this.propertyName = propertyName; 
     this.propertyValue = propertyValue; 
    } 
... 
... 
... 
} 

这是结果:

[{"id":"542c00c2ff5e0ba4ea58790d","propertyName":"property1","propertyValue":null},{"id":"542c00c2ff5e0ba4ea58790e","propertyName":"property2","propertyValue":null},{"id":"542c00c2ff5e0ba4ea58790f","propertyName":"property3","propertyValue":null}] 

我使用Spring持久层的数据。我尝试使用JSONIgnore注释和类似的东西,但没有为我工作。 任何帮助将受到欢迎。

在此先感谢。

回答

2

尝试这种方式将其标注为:

@JsonInclude(Include.NON_EMPTY) 
public class PropertyEntity implements GenericEntity { 
+0

正如我评论我试过,但它没有工作。无论如何感谢您的帮助。 – ginxo 2014-10-02 11:49:22