title: What's the proper way to escape URL variables with Spring's RestTemplate when calling a Spring RestController?
body: <p>When calling <code>RestTemplate.exchange</code> to do a get request, such as:</p>
  
  <pre><code>String foo = "fo+o";
  String bar = "ba r";
  restTemplate.exchange("http://example.com/?foo={foo}&amp;bar={bar}", HttpMethod.GET, null, foo, bar)
  </code></pre>
  
  <p>what's the proper to have the URL variables correctly escaped for the get request?</p>
  
  <p>Specifically, how do I get pluses (<code>+</code>) correctly escaped because <a href="https://stackoverflow.com/questions/50270372/why-is-spring-de-coding-the-plus-character-on-application-json-get-requests">Spring is interpreting as spaces</a>, so, I need to encode them.</p>
  
  <p>I tried using <code>UriComponentsBuilder</code> like this:</p>
  
  <pre><code>String foo = "fo+o";
  String bar = "ba r";
  UriComponentsBuilder ucb = UriComponentsBuilder.fromUriString("http://example.com/?foo={foo}&amp;bar={bar}");
  System.out.println(ucb.build().expand(foo, bar).toUri());
  System.out.println(ucb.build().expand(foo, bar).toString());
  System.out.println(ucb.build().expand(foo, bar).toUriString());
  System.out.println(ucb.build().expand(foo, bar).encode().toUri());
  System.out.println(ucb.build().expand(foo, bar).encode().toString());
  System.out.println(ucb.build().expand(foo, bar).encode().toUriString());
  System.out.println(ucb.buildAndExpand(foo, bar).toUri());
  System.out.println(ucb.buildAndExpand(foo, bar).toString());
  System.out.println(ucb.buildAndExpand(foo, bar).toUriString());
  System.out.println(ucb.buildAndExpand(foo, bar).encode().toUri());
  System.out.println(ucb.buildAndExpand(foo, bar).encode().toString());
  System.out.println(ucb.buildAndExpand(foo, bar).encode().toUriString());
  </code></pre>
  
  <p>and that printed:</p>
  
  <pre><code>http://example.com/?foo=fo+o&amp;bar=ba%20r
  http://example.com/?foo=fo+o&amp;bar=ba r
  http://example.com/?foo=fo+o&amp;bar=ba r
  http://example.com/?foo=fo+o&amp;bar=ba%20r
  http://example.com/?foo=fo+o&amp;bar=ba%20r
  http://example.com/?foo=fo+o&amp;bar=ba%20r
  http://example.com/?foo=fo+o&amp;bar=ba%20r
  http://example.com/?foo=fo+o&amp;bar=ba r
  http://example.com/?foo=fo+o&amp;bar=ba r
  http://example.com/?foo=fo+o&amp;bar=ba%20r
  http://example.com/?foo=fo+o&amp;bar=ba%20r
  http://example.com/?foo=fo+o&amp;bar=ba%20r
  </code></pre>
  
  <p>The space is correctly escaped in some instances, but the plus is never escaped.</p>
  
  <p>I also tried <code>UriTemplate</code> like this:</p>
  
  <pre><code>String foo = "fo+o";
  String bar = "ba r";
  UriTemplate uriTemplate = new UriTemplate("http://example.com/?foo={foo}&amp;bar={bar}");
  Map&lt;String, String&gt; vars = new HashMap&lt;&gt;();
  vars.put("foo", foo);
  vars.put("bar", bar);
  URI uri = uriTemplate.expand(vars);
  System.out.println(uri);
  </code></pre>
  
  <p>with the exact same result:</p>
  
  <pre><code>http://example.com/?foo=fo+o&amp;bar=ba%20r
  </code></pre>