0
比方說,當URL包含有效的JSON時,我需要使用RestTemplate作爲GET請求。一個例子URL是:當URL包含JSON時處理RestTemplate的模式匹配
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 。從實驗中,我知道花括號是問題所在。但是,使用反斜槓轉義它們並不能解決問題。下面的代碼產生「未封閉組」異常:
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正確引用網址是什麼?
爲什麼你需要納入'json' url中,如果它是一個'GET'要求? – NiVeR
不幸的是,API就是這麼設計的...... – Ozilophile
正如David S.指出的那樣,URL編碼是關鍵。然而,質疑API設計。另請注意,瀏覽器/服務器確實有字符限制。使用POST方法的請求體將更有意義 – Hirako