SpringRestTemplate使用参数获取

我必须进行一个REST调用,其中包括自定义标题和查询参数。我只使用头(无正文)设置我的HttpEntity,并使用restemplate.exchange()方法如下:

HttpHeaders=newhttpheaders();
headers.set(“接受”、“应用程序/json”);
地图<字符串,字符串>params=新的HashMap<字符串,字符串>();
参数put(“msisdn”,msisdn);
参数put(“电子邮件”,电子邮件);
参数put(“clientVersion”,clientVersion);
参数put(“clientType”,clientType);
params.put(“issuerName”,issuerName);
参数put(“applicationName”,applicationName);
HttpEntity=新的HttpEntity(标题);
HttpEntity<字符串>response=restemplate.exchange(url,HttpMethod.GET,entity,String.class,params);

这在客户端失败,因为dispatcher servlet无法将请求解析为处理程序。调试完成后,似乎没有发送请求参数

当我使用一个请求体和一个POST进行交换时,没有查询参数,它工作得很好

有人有什么想法吗

要轻松操作URL/path/params/等,可以使用Spring的UriComponentsBuilder类。它比手动连接字符串更简洁,并且为您处理URL编码:

HttpHeaders=newhttpheaders();
set(HttpHeaders.ACCEPT,MediaType.APPLICATION\u JSON\u值);
UriComponentsBuilder=UriComponentsBuilder.fromHttpUrl(url)
.queryParam(“msisdn”,msisdn)
.queryParam(“电子邮件”,电子邮件)
.queryParam(“客户端版本”,客户端版本)
.queryParam(“clientType”,clientType)
.queryParam(“issuerName”,issuerName)
.queryParam(“applicationName”,applicationName);
HttpEntity&lt&燃气轮机;实体=新的HttpEntity&lt&燃气轮机;(标题);
HttpEntity<字符串>response=restTemplate.exchange(
builder.string(),
HttpMethod.GET,
实体
字符串(类);

发表评论