Let's say I need to use RestTemplate for a GET request, when the URL contains valid JSON. An example URL is:
http://example.com/?json={"property1":{"property2":0},"property3":{"property4":0.0,"property5":0.0}}
The code:
String url = "http://example.com/?json={\"property1\":{\"property2\":0},\"property3\":{\"property4\":0.0,\"property5\":0.0}}";
RestTemplate template = new RestTemplate();
ResponseEntity<String> response = template.getForEntity(url, String.class);
The Exception:
Exception in thread "main" java.util.regex.PatternSyntaxException: Illegal repetition near index 32
\Qhttp://example.com/?json=\E({"property2":0)\Q,"property3":\E(0.0,"property5":0.0)\Q}\E
^
at java.util.regex.Pattern.error(Pattern.java:1955)
at java.util.regex.Pattern.closure(Pattern.java:3157)
at java.util.regex.Pattern.sequence(Pattern.java:2134)
at java.util.regex.Pattern.expr(Pattern.java:1996)
at java.util.regex.Pattern.group0(Pattern.java:2905)
at java.util.regex.Pattern.sequence(Pattern.java:2051)
at java.util.regex.Pattern.expr(Pattern.java:1996)
at java.util.regex.Pattern.compile(Pattern.java:1696)
at java.util.regex.Pattern.<init>(Pattern.java:1351)
at java.util.regex.Pattern.compile(Pattern.java:1028)
at org.springframework.web.util.UriTemplate$Parser.getMatchPattern(UriTemplate.java:247)
at org.springframework.web.util.UriTemplate$Parser.access$200(UriTemplate.java:196)
at org.springframework.web.util.UriTemplate.<init>(UriTemplate.java:70)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:471)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:260)
RestTemplate is quoting the URL up to the beginning of the JSON, instead of the entire URL. From experimenting, I know that the curly braces are the issue. However, escaping them using a backslash does not solve the problem. The following code produces an "unclosed group" exception:
String url = "http://example.com/?json=\\{\"property1\":\\{\"property2\":0\\}\\}";
RestTemplate template = new RestTemplate();
ResponseEntity<String> response = template.getForEntity(url, String.class);
The exception:
Exception in thread "main" java.util.regex.PatternSyntaxException: Unclosed group near index 56
\Qhttp://example.com/?json=\\E(\{"property2":0\)\Q\}\E
^
at java.util.regex.Pattern.error(Pattern.java:1955)
at java.util.regex.Pattern.accept(Pattern.java:1813)
at java.util.regex.Pattern.group0(Pattern.java:2908)
at java.util.regex.Pattern.sequence(Pattern.java:2051)
at java.util.regex.Pattern.expr(Pattern.java:1996)
at java.util.regex.Pattern.compile(Pattern.java:1696)
at java.util.regex.Pattern.<init>(Pattern.java:1351)
at java.util.regex.Pattern.compile(Pattern.java:1028)
at org.springframework.web.util.UriTemplate$Parser.getMatchPattern(UriTemplate.java:247)
at org.springframework.web.util.UriTemplate$Parser.access$200(UriTemplate.java:196)
at org.springframework.web.util.UriTemplate.<init>(UriTemplate.java:70)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:471)
at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:260)
at com.ebay.sdo.misc.ThemisTest.main(ThemisTest.java:55)
How can I make RestTemplate quote the URL correctly?
you are trying to pass the JSON string directly in URL, breaking the url encoding. You should url encode your JSON payload before you use it in resttemplate.
Refer this question, to learn several ways of how to urlencode: