问题是我想隐藏RESTFul JSON响应中的null元素(如果可能的话).
REST控制器从Mongo数据库中检索信息,因为这些元素不存在,我想在它们为空时忽略它们.
这是我的REST控制器(用Jersey公开):
@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 Data用作持久层.我尝试使用JSONIgnore注释和类似的东西,但没有任何东西适合我.
欢迎任何帮助.
提前致谢.
解决方法:
尝试以这种方式注释:
@JsonInclude(Include.NON_EMPTY)
public class PropertyEntity implements GenericEntity {