Java Spring RestTemplate传递响应的类型
我正在使用SpringRESTTemplate对请求主体进行GET调用 因此,我使用rest模板进行了一系列web服务调用。对于它们中的每一个,它们都支持获取作为请求体参数传递查询的资源。我使用以下方法通过身体:Java Spring RestTemplate传递响应的类型,java,spring,rest,resttemplate,Java,Spring,Rest,Resttemplate,我正在使用SpringRESTTemplate对请求主体进行GET调用 因此,我使用rest模板进行了一系列web服务调用。对于它们中的每一个,它们都支持获取作为请求体参数传递查询的资源。我使用以下方法通过身体: Map<String, String> requestBody = new HashMap<String, String>(); requestBody.put("type", "Sandwich"); requestBody.put("flavor",
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);
但是,当我查看服务器端收到的请求时,根本没有传递正文。rest模板是否不支持为GET调用传递正文?正文的可能副本根本没有传递,或者它没有像您预期的那样传递?具体来说,您的服务器收到的请求中有什么内容?没有任何正文是不被传递的。服务器可以看到标题。Body为null。有哪些头值?我在基类中将内容类型作为application/json传递,就是这样。headers=新的HttpHeaders();headers.setContentType(MediaType.APPLICATION_JSON);
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;
}
{ "query": [ { "field" : "type", "value": "Sandwich" } , { "field" : "flavor", "value": "blueberry" }] }