带请求参数的 RestTemplate GET 请求
我必须调用 REST Web 服务,并且我计划使用 RestTemplate。我查看了有关如何发出 GET 请求的示例,如下所示。
String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", String.class,"42","21");
就我而言,RESTful url 如下所示。在这种情况下我该如何使用 RestTemplate?
http://example.com/hotels?state=NY&country=USA
所以我的问题是如何发送 GET 请求的请求参数?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在向 RESTful 服务器发出请求时,在许多情况下需要发送查询参数、请求正文(在
POST
和PUT
请求方法的情况下)以及向服务器发出的请求中的标头。在这种情况下,可以使用 UriComponentsBuilder.build(),使用UriComponents。如果需要,请进行编码(),并使用 RestTemplate.exchange() 像这样:
While making a request to a RESTful server, it requires in many a cases to send query parameters, request body (in case of
POST
andPUT
request methods), as well as headers in the request to the server.In such cases, the URI string can be built using UriComponentsBuilder.build(), encoded using UriComponents.encode() if needed to, and sent using RestTemplate.exchange() like this:
占位符对于任何类型的 url 的工作方式都是相同的,只是
这样做或者更好,使用哈希图进行实名匹配 -
the placeholders work the same for either type of url, just do
or better yet, use a hashmap for real name matching-