Java RestTemplate:如何将 URL 和查询参数一起发送
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35998790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RestTemplate: How to send URL and query parameters together
提问by Shiva
I am trying to pass path param and query params in a URL but I am getting a weird error. below is the code
我试图在 URL 中传递路径参数和查询参数,但我收到一个奇怪的错误。下面是代码
String url = "http://test.com/Services/rest/{id}/Identifier"
Map<String, String> params = new HashMap<String, String>();
params.put("id", "1234");
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url)
.queryParam("name", "myName");
String uriBuilder = builder.build().encode().toUriString();
restTemplate.exchange(uriBuilder , HttpMethod.PUT, requestEntity,
class_p, params);
and my url is becoming http://test.com/Services/rest/%7Bid%7D/Identifier?name=myName
我的网址正在变成 http://test.com/Services/rest/%7Bid%7D/Identifier?name=myName
what should I do to make it work. I am expecting http://test.com/Services/rest/{id}/Identifier?name=myName
so that params will add id to the url
我该怎么做才能让它发挥作用。我期待http://test.com/Services/rest/{id}/Identifier?name=myName
这样 params 将 id 添加到 url
please suggest. thanks in Advance
请建议。提前致谢
采纳答案by Michal Foksa
I would use buildAndExpand
from UriComponentsBuilder
to pass all types of URI parameters.
我会使用buildAndExpand
fromUriComponentsBuilder
来传递所有类型的 URI 参数。
For example:
例如:
String url = "http://test.com/solarSystem/planets/{planet}/moons/{moon}";
// URI (URL) parameters
Map<String, String> urlParams = new HashMap<>();
urlParams.put("planets", "Mars");
urlParams.put("moons", "Phobos");
// Query parameters
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(url)
// Add query parameter
.queryParam("firstName", "Mark")
.queryParam("lastName", "Watney");
System.out.println(builder.buildAndExpand(urlParams).toUri());
/**
* Console output:
* http://test.com/solarSystem/planets/Mars/moons/Phobos?firstName=Mark&lastName=Watney
*/
restTemplate.exchange(builder.buildAndExpand(urlParams).toUri() , HttpMethod.PUT,
requestEntity, class_p);
/**
* Log entry:
* org.springframework.web.client.RestTemplate Created PUT request for "http://test.com/solarSystem/planets/Mars/moons/Phobos?firstName=Mark&lastName=Watney"
*/
回答by holmis83
An issue with the answer from Michal Foksais that it adds the query parameters first, and then expands the path variables. If query parameter contains parenthesis, e.g. {foobar}
, this will cause an exception.
Michal Foksa的答案的一个问题是它首先添加查询参数,然后扩展路径变量。如果查询参数包含括号,例如{foobar}
,这将导致异常。
The safe way is to expand the path variables first, and then add the query parameters:
安全的方法是先展开路径变量,然后添加查询参数:
String url = "http://test.com/Services/rest/{id}/Identifier";
Map<String, String> params = new HashMap<String, String>();
params.put("id", "1234");
URI uri = UriComponentsBuilder.fromUriString(url)
.buildAndExpand(params)
.toUri();
uri = UriComponentsBuilder
.fromUri(uri)
.queryParam("name", "myName")
.build()
.toUri();
restTemplate.exchange(uri , HttpMethod.PUT, requestEntity, class_p);
回答by K. O.
One-liner using TestRestTemplate.exchange function with parameters map.
使用带有参数映射的 TestRestTemplate.exchange 函数的单线。
restTemplate.exchange("/someUrl?id={id}", HttpMethod.GET, reqEntity, respType, ["id": id])
The params map initialized like this is a groovyinitializer*
像这样初始化的 params 映射是一个groovy初始值设定项*