Hide JSON Fields in RESTful Jersey

The thing is, I want to hide the elements nullfrom the RESTFul JSON response (if possible). The REST controller retrieves information from the Mongo database and because these elements do not exist, I would like to ignore them when they are null.

This is my REST controller (exposed from 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();
    }

    ...
    ...
    ...
}

This is my essence:

@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;
    }
...
...
...
}

And this is the result:

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

I am using Spring Data for the persistence layer. I tried using JSONIgnore annotations and the like, but nothing works for me. Any help would be appreciated.

Thanks in advance.

+4
source share
1 answer

Try annotating it like this:

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

All Articles