0

I use Java 8 application based on springboot and gradle and i try to suppress all null values into my JSON request body :

{"typePersonne":"Adherent","adressePrincipale":{"ligne1":null,"ligne2":null}

I have create interceptor to see my json query :

public class RequestResponseLoggingInterceptor implements ClientHttpRequestInterceptor {
private final org.slf4j.Logger log = LoggerFactory.getLogger(this.getClass());

private void logRequest(final HttpRequest request, final byte[] body) throws IOException {
    if (log.isDebugEnabled()) {
        log.debug("===========================request begin================================================");
        log.debug("URI         : {}", request.getURI());
        log.debug("Method      : {}", request.getMethod());
        log.debug("Headers     : {}", request.getHeaders());
        log.debug("Request body: {}", new String(body, "UTF-8"));
        log.debug("==========================request end================================================");
    }
}

private void logResponse(final ClientHttpResponse response) throws IOException {
    if (log.isDebugEnabled()) {
        log.debug("============================response begin==========================================");
        log.debug("Status code  : {}", response.getStatusCode());
        log.debug("Status text  : {}", response.getStatusText());
        log.debug("Headers      : {}", response.getHeaders());
        log.debug("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset()));
        log.debug("=======================response end=================================================");
    }
}

@Override
public ClientHttpResponse intercept(final HttpRequest request, final byte[] body, final ClientHttpRequestExecution execution) throws IOException {
    logRequest(request, body);
    final ClientHttpResponse response = execution.execute(request, body);
    logResponse(response);
    return response;
}

}

How can i suppress all key corresponding to the null values when i call the API ?

final HttpEntity<?> httpEntity = new HttpEntity<>(adherent, headersCompleted);

responseEntity = restTemplate.exchange(urlComplete, HttpMethod.PUT, httpEntity,String.class);

Thanks :)

Louis Jacomet
  • 13,661
  • 2
  • 34
  • 43
Jérémy
  • 391
  • 2
  • 7
  • 20
  • When do you want to suppress these keys? When logging? Or when you get the value back as the responseEntity? – Tom Cools Apr 14 '20 at 15:29
  • Are you using any library like Jackson to build your JSON requests? Such libraries have facilities to ignore null and/or empty values. – Nowhere Man Apr 14 '20 at 15:31
  • I want to suppress all null when i call API with the restTemplate.exchange. – Jérémy Apr 14 '20 at 15:53
  • this is a springboot projet so i suppose it use jackson. How can i check that ? I have try to add default-property-inclusion: non_null in my gradle yml conf but there is no change. – Jérémy Apr 14 '20 at 15:57
  • `@JsonInclude(JsonInclude.Include.NON_NULL)` is only for serialization so during response rendering for e.g. Here you talk about request so you want to exclude null during deserialization .. right? – CodeScale Apr 14 '20 at 16:51
  • Yes, this is for sending request – Jérémy Apr 14 '20 at 17:48
  • Have you tried `spring.jackson.default-property-inclusion = NON_NULL` ?(application.properties ... https://stackoverflow.com/a/36515285/592355) – xerx593 Apr 15 '20 at 12:31
  • ..and what you do is somewhat "odd": you "exchange for a string" (the raw external response) ..and expect null values excluded. If the above not works, you must map to (your custom) objects, exclude nulls(...), ...and serialize these. – xerx593 Apr 15 '20 at 12:39

0 Answers0