If URIComponentsBuilder is used to create uri that contains some json as query param, it will not work as JSON will not be encoded. This is probably due to json curly braces which are mistakenly taken as var placeholder.

@Test
public void jsonQueryParamIsEscaped() {
    UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/some/path")
            .queryParam("json", "{\"foo\":\"bar\"}")
            .queryParam("mustBeEscaped", "? # & \" ")
            .queryParam("escapedDoubleQuotes", "\"")
            .encode();
    String toUriString = builder.build().toUriString();
    assertThat(toUriString).isEqualTo("/some/path?json=%257B%2522foo%2522%253A%2522bar%2522%257D&mustBeEscaped=?%20%23%20%26%20%22%20&escapedDoubleQuotes=%22");
}
org.opentest4j.AssertionFailedError: 
expected: "/some/path?json=%257B%2522foo%2522%253A%2522bar%2522%257D&mustBeEscaped=?%20%23%20%26%20%22%20&escapedDoubleQuotes=%22"
 but was: "/some/path?json={"foo":"bar"}&mustBeEscaped=?%20%23%20%26%20%22%20&escapedDoubleQuotes=%22"

Is is documented somewhere? Is there is a way to escape {} in query ? I have tried \{} but to no avail

Comment From: jaoromi

You should use build().encode() instead of encode().build(). The encode method is responsible for pre-compiling the URI template and expanding the variables.

        UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/some/path")
                .queryParam("json", "{\"foo\":\"bar\"}")
                .queryParam("mustBeEscaped", "? # & \" ")
                .queryParam("escapedDoubleQuotes", "\"");
        String toUriString = builder.build().encode().toUriString();

Comment From: rstoyanchev

You can use a URI variable for the JSON content:

UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/some/path")
        .queryParam("json", "{jsonContent}")
        .queryParam("mustBeEscaped", "? # & \" ")
        .queryParam("escapedDoubleQuotes", "\"")
        .encode();
String toUriString = builder.build().expand("{\"foo\":\"bar\"}").toUriString();

This is the part of the documentation.