我正在使用Spring RestTemplate与请求正文进行GET调用。
所以我正在使用其余模板进行一系列Web服务调用。对于它们中的每一个,它们都支持获取将查询作为请求主体参数传递的资源。我使用以下内容传递身体:
Map<String, String> requestBody = new HashMap<String, String>();
requestBody.put("type", "Sandwich");
requestBody.put("flavor", "blueberry");
MultiValueMap<String, Map<String, String>> body = constructQueries(requestBody); //see below
HttpEntity request = new HttpEntity(body, headers);
ResponseEntity<T> response = restTemplate.exchange("http://myurl.com/ids", HttpMethod.GET, request, type);
我按照以下方式构建正文:
private MultiValueMap<String, Map<String, String>> constructQueries(Map<String, String> pBody){
MultiValueMap<String, Map<String, String>> body = new LinkedMultiValueMap<String, Map<String, String>>();
//List<Map<String, String>> queryLists = new ArrayList<Map<String, String>>();
for (Map.Entry<String, String> entry : pBody.entrySet())
{
Map<String, String> singleQuery = new HashMap<String, String>();
singleQuery.put("field", entry.getKey());
singleQuery.put("value", entry.getValue());
body.add("query", singleQuery);
}
return body;
}
我的身体(用JSON)应该是这样的:
{ "query": [ { "field" : "type", "value": "Sandwich" } , { "field" : "flavor", "value": "blueberry" }] }
但是当我看到服务器端收到的请求时,身体根本没有通过。休息模板不支持GET调用的传递体吗?