当URL包含有效的JSON时,我们需要使用RestTemplate进行GET请求。示例URL是:
http://example.com/?json={"property1":{"property2":0},"property3":{"property4":0.0,"property5":0.0}}
代码:
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);
例外:
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引用的URL直到JSON的开头,而不是整个URL。从实验来看,我知道花括号是个问题。但是,使用反斜杠转义它们并不能解决问题。以下代码生成一个&#34;未关闭的组&#34;例外:
String url = "http://example.com/?json=\\{\"property1\":\\{\"property2\":0\\}\\}";
RestTemplate template = new RestTemplate();
ResponseEntity<String> response = template.getForEntity(url, String.class);
例外:
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)
如何让RestTemplate正确引用网址?
答案 0 :(得分:1)
您正试图直接在URL中传递JSON字符串,从而破坏了网址编码。在resttemplate中使用它之前,你应该对你的JSON有效负载进行url编码。
请参阅此问题,了解如何使用urlencode的几种方法: