title: Encoding query parameters with UriComponentsBuilder
body: <p>I am struggling to understand the behavior of <a href="https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/web/util/UriComponentsBuilder.html" rel="noreferrer"><code>UriComponentsBuilder</code></a>. I want to use it to encode a URL in a query parameter, however it appears to only escape <code>%</code> characters, but not other necessary characters such as <code>&amp;</code>.</p>
  
  <p>An example of a URL in a query parameter that is not encoded at all:</p>
  
  <pre><code>UriComponentsBuilder.fromUri("http://example.com/endpoint")
                      .queryParam("query", "/path?foo=foo&amp;bar=bar")
                      .build();
  </code></pre>
  
  <p>Output: <code>http://example.com/endpoint?query=/path?foo=foo&amp;bar=bar</code></p>
  
  <p>This is not correct, because the unencoded <code>&amp;</code> causes <code>bar=bar</code> to be interpreted as a query parameter to <code>/endpoint</code> instead of <code>/path</code>.</p>
  
  <p>However, if I use an input that contains a <code>%</code> character::</p>
  
  <pre><code>UriComponentsBuilder.fromUri("http://example.com/endpoint")
                      .queryParam("query", "/path?foo=%20bar")
                      .build();
  </code></pre>
  
  <p>Output: <code>http://example.com/endpoint?query=/path?foo=%2520bar</code></p>
  
  <p>The <code>%</code> character is escaped.</p>
  
  <p>It seems inconsistent that <code>UriComponentsBuilder</code> would automatically escape the <code>%</code> characters but not the other reserved characters.</p>
  
  <p>What is the correct process to encode a URL into a query parameter with <code>UriComponentsBuilder</code>?</p>