当使用 RestTemplate 可能会遇到异常:java
Not enough variables available to expand
典型以下:json
@Autowired private RestTemplate restTemplate; String url = "http://localhost:8080/search?people={\"name\":\"jack\",\"age\":18}"; String email = restTemplate.getForObject(url, String.class);
这样使用,会出现以下报错信息:url
Exception in thread "main" java.lang.IllegalArgumentException: Not enough variable values available to expand '"name"'
这个地方很使人费解,难道不能这样使用?通过一顿查找,发现原来是由于。。。.net
url由于自己的缘由,把花括号 { } 中的内容当成了占位符,而这里又没有明确说明占位符对应的值,因此会致使报错。rest
只须要简单几步便可解决。在url中使用占位符,将占位符的值即所传 json 放在第3个参数位置。
以下:code
String json = {"\"name\":\"jack\",\"age\":18"}; String url = "http://localhost:8080/search?people={json}"; String email = restTemplate.getForObject(url, String.class, json);
这样处理以后,就能够正常使用了。blog
原文:https://blog.csdn.net/ezreal_king/article/details/72654440get