json - 在 Jersey RESTful 中隐藏 JSON 字段

标签 json spring mongodb rest jersey

问题是我想从 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/

相关文章:

python - 加载 JSON 并获取某些数据 (Python)

json - swift 中的 AlamofireObjectMapper

android - 改造 : How to make XML request and get back JSON response

java - 如何使用 Spring Data JPA 从列中获取链接和值?

java - 如何在 spring 中覆盖 JndiObjectFactoryBean 并在 java 中设置解密密码

node.js - 错误 ReferenceError : ObjectID is not defined

php - Yii2 Active record 如何创建 mongo 模型之间的关系

c# - Json反序列化形式Dictionary<string, Dictionary<string, string>>

java - @WebMvcTest 失败,出现 java.lang.IllegalStateException : Failed to load ApplicationContext

javascript - node.js mongojs findOne 回调返回错误为空