问题是我想从 RESTFul JSON 响应中隐藏 null
元素(如果可能的话)。
REST Controller 从 Mongo 数据库中检索信息,因为该元素在那里不存在,所以当它们为 null
时我想忽略它们。
这是我的 REST Controller (暴露在 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 {
关于json - 在 Jersey RESTful 中隐藏 JSON 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26143634/