微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在Spring中使用RestTemplate异常-没有足够的变量可用于展开 报错 拼音 双语对照

如何解决在Spring中使用RestTemplate异常-没有足够的变量可用于展开 报错 拼音 双语对照

根本原因是将给定URL中的RestTemplate花括号{...}视为URI变量的占位符,并尝试根据其名称替换它们。例如

{pageSize}

会尝试获取名为的URI变量pageSize。这些URI变量是通过其他一些重载getForObject方法指定的。你没有提供任何内容,但你的URL需要一个,因此该方法将引发异常。

一种解决方案是使String包含该值的对象

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。任何帮助都感激不尽。

谢谢

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。