码迷,mamicode.com
首页 > 其他好文 > 详细

RestTemplate的异常 Not enough variables available to expand

时间:2020-01-29 21:24:42      阅读:79      评论:0      收藏:0      [点我收藏+]

标签:art   value   string   arch   available   object   thread   code   get   

当使用 RestTemplate 可能会遇到异常:

Not enough variables available to expand

典型如下:

@Autowired
private RestTemplate restTemplate;

String url = "http://localhost:8080/search?people={\"name\":\"jack\",\"age\":18}";

String email = restTemplate.getForObject(url, String.class);

这样使用,会出现如下报错信息:

Exception in thread "main" java.lang.IllegalArgumentException: Not enough variable values available to expand '"name"'

这个地方很令人费解,难道不能这样使用?经过一顿查找,发现原来是因为。。。

url因为本身的原因,把花括号 { } 中的内容当成了占位符,而这里又没有明确说明占位符对应的值,所以会导致报错。

只需要简单几步即可解决。在url中使用占位符,将占位符的值即所传 json 放在第3个参数位置。
如下:

String json = {"\"name\":\"jack\",\"age\":18"};
String url = "http://localhost:8080/search?people={json}";
String email = restTemplate.getForObject(url, String.class, json);

这样处理之后,就可以正常使用了。

参考:

原文:https://blog.csdn.net/ezreal_king/article/details/72654440

RestTemplate的异常 Not enough variables available to expand

标签:art   value   string   arch   available   object   thread   code   get   

原文地址:https://www.cnblogs.com/eternityz/p/12241395.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!