4

What I'm trying to do is to avoid null fields in my request. I use this Jersey dependencies

    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-client</artifactId>
        <version>2.5</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.18</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-multipart</artifactId>
        <version>1.17</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-json-jackson</artifactId>
        <version>2.5.1</version>
    </dependency>

Here is my Jersey configuration

Client client = ClientBuilder
        .newClient()
        .register(authenticationFeature)
        .register(CustomTypeProvider.class)
        .register(MultiPartWriter.class)
        .register(JacksonFeature.class);

And that's what I'm trying to send

{
    "locale": "en_US",
    "file": {
        "file": {
            "version": null,
            "permissionMask": null,
            "creationDate": null,
            "updateDate": null,
            "label": "my.properties",
            "description": null,
            "uri": null,
            "type": "prop",
            "content": null
        }
    }
}

but I need

   {
        "locale": "en_US",
        "file": {
            "file": {
                "label": "my.properties",
                "type": "prop",
            }
        }
    }

How can I exclude all null fields form my request?

Alex
  • 2,091
  • 6
  • 31
  • 49

2 Answers2

6

I beleive, that jersey is using jackson for serialization. For excluding null fields from serialized json, try to annotate the target class with @JsonInclude(Include.NON_NULL). As explained in this post.

If you can't change entities, you must configure your custom ObjectMapper:

@Provider
public class MyObjectMapperProvider implements ContextResolver<ObjectMapper> {

    final ObjectMapper mapper;

    public MyObjectMapperProvider() {
        mapper = new ObjectMapper();
        mapper.setSerializationInclusion(Include.NON_NULL);
    }

    @Override
    public ObjectMapper getContext(Class<?> type) {
        return mapper
    }
}

then register your custom provider to the client:

Client client = ClientBuilder
    .newClient()
    .register(MyObjectMapperProvider.class)
    .register(JacksonFeature.class);

its described here

Atul Kumbhar
  • 1,073
  • 16
  • 26
Tomas Bartalos
  • 1,256
  • 12
  • 29
  • But I can't change entities. All setup must perform on Jersey side. – Alex Sep 21 '14 at 15:03
  • 1
    Then you need to configure your ObjectMapper directly via `mapper.setSerializationInclusion(Include.NON_NULL);`. Here is an explanation how to do it for jersey: http://wiki.fasterxml.com/JacksonFAQJaxRs . You need to write your custom ContextResolver, which will provide your fine-tuned ObjectMapper. Then register it to the client instance from your example – Tomas Bartalos Sep 21 '14 at 23:03
0

Alternative for Jersey 1.x:

org.codehaus.jackson.map.ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(Inclusion.NON_NULL);
dvlcube
  • 1,117
  • 1
  • 12
  • 20