title: How to dynamically remove empty query parameters?
body: <p>I am sending some get query to an API that returns a <strong>400 Bad request</strong> when I send empty query parameters.</p>
  <p>However, I do not know in advance which optional query parameters will be empty.
  Apart from testing each optional parameter for emptyness, is there a simpler approach to dynamically remove empty parameters ?</p>
  <p>Currently I am doing:</p>
  <pre><code>private URI getApiUrl(QueryParametersObject qpo) {
      String url = configBundle.getPropertyStr(&quot;some.api.url&quot;);
      UriComponentsBuilder builder = UriComponentsBuilder
              .fromHttpUrl(url)
              .queryParam(&quot;mandatoryParam1&quot;, qpo.getMandatoryParam1())  // MANDATORY
              .queryParam(&quot;mandatoryParam2&quot;, qpo.getMandatoryParam2());  // MANDATORY
              
      if (!qpo.getOptionalParam1().isEmpty())
          builder.queryParam(&quot;optionalParam1&quot;, qpo.getOptionalParam1());
      if (!qpo.getOptionalParam2().isEmpty())
          builder.queryParam(&quot;optionalParam2&quot;, qpo.getOptionalParam2());
      if (!qpo.getOptionalParam3().isEmpty())
          builder.queryParam(&quot;optionalParam3&quot;, qpo.getOptionalParam3());
      return builder.build().encode().toUri();
  }
  </code></pre>
  <p>It can become quite cumbersome when the number of optional query parameters grows.</p>