Spring RestTemplate GET参数 [英] Spring RestTemplate GET with parameters
问题描述
我必须进行包含自定义标头和查询参数的 REST
调用。我只用标题(没有正文)设置我的 HttpEntity
,并使用 RestTemplate.exchange()
方法作为如下:
I have to make a REST
call that includes custom headers and query parameters. I set my HttpEntity
with just the headers (no body), and I use the RestTemplate.exchange()
method as follows:
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", "application/json");
Map<String, String> params = new HashMap<String, String>();
params.put("msisdn", msisdn);
params.put("email", email);
params.put("clientVersion", clientVersion);
params.put("clientType", clientType);
params.put("issuerName", issuerName);
params.put("applicationName", applicationName);
HttpEntity entity = new HttpEntity(headers);
HttpEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class, params);
在客户端使用调度程序servlet $ c $失败c>无法解析对处理程序的请求。调试后,看起来似乎没有发送请求参数。
This fails at the client end with the dispatcher servlet
being unable to resolve the request to a handler. Having debugged it, it looks like the request parameters are not being sent.
当我用 POST $ c $进行交换时c>使用请求正文并且没有查询参数它可以正常工作。
When I do a an exchange with a POST
using a request body and no query parameters it works just fine.
有没有人有任何想法?
推荐答案
好的,所以我是个白痴,我把查询参数与url参数混淆了。我有点希望有一个更好的方法来填充我的查询参数而不是一个丑陋的连接字符串,但我们有。这只是用正确的参数构建URL的一种情况。如果你把它作为一个String Spring传递,你也会为你处理编码。
OK, so I'm being an idiot and I'm confusing query parameters with url parameters. I was kinda hoping there would be a nicer way to populate my query parameters rather than an ugly concatenated String but there we are. It's simply a case of build the URL with the correct parameters. If you pass it as a String Spring will also take care of the encoding for you.
这篇关于Spring RestTemplate GET参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!