大佬教程收集整理的这篇文章主要介绍了在Spring中使用RestTemplate。异常-没有足够的变量可用于展开 报错 拼音 双语对照,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
根本原因是将给定URL中的RestTemplate
花括号{...}
视为URI变量的占位符,并尝试根据其名称替换它们。例如
{pageSizE}
会尝试获取名为的URI变量pageSize
。这些URI变量是通过其他一些重载getForObject方法指定的。你没有提供任何内容,但你的URL需要一个,因此该方法将引发异常。
String sort = "{\"price\":\"desc\"}";
并在你的网址中提供真实的URI变量
String url1 = "http://API.example.com/Search?key=52ddafbe3ee659bad97fcce7c53592916a6bfd73&term=&limit=100&sort={sort}";
你会打电话给你getForObject()
,像这样
OutputPage page = restTemplate.getForObject(url1, OutputPage.class, sort);
我强烈建议你不要在GET请求的请求参数中发送任何JsON,而应在POST请求的正文中发送。
我正在尝试访问API的内容,我需要使用RestTemplate发送URL。
String url1 = "http://api.example.com/Search?key=52ddafbe3ee659bad97fcce7c53592916a6bfd73&term=&limit=100&sort={\"price\":\"desc\"}";
OutputPage page = restTemplate.getForObject(url1,OutputPage .class);
但是,我收到以下错误。
Exception in thread "main" java.lang.IllegalArgumentexception: Not enough variable values available to expand '"price"'
at org.springframework.web.util.UriComponents$VarArgsTemplateVariables.getValue(UriComponents.java:284)
at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:220)
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:317)
at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:46)
at org.springframework.web.util.UriComponents.expand(UriComponents.java:162)
at org.springframework.web.util.UriTemplate.expand(UriTemplate.java:119)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:501)
at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:239)
at Hello.Application.main(Application.java:26)
如果我删除排序条件,则它工作正常。我需要使用排序条件来解析JSON。任何帮助都感激不尽。
谢谢
以上是大佬教程为你收集整理的在Spring中使用RestTemplate。异常-没有足够的变量可用于展开 报错 拼音 双语对照全部内容,希望文章能够帮你解决在Spring中使用RestTemplate。异常-没有足够的变量可用于展开 报错 拼音 双语对照所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。