<分区>
Possible Duplicate:
HTTP GET with request body
我在这里读过一些不提倡通过 HTTP GET 发送内容的讨论。可以通过客户端(Web 浏览器)发送的数据大小有限制。并且处理 GET 数据也依赖于服务器。请参阅下面的资源部分。
但是,有人要求我测试使用 RestTemplate 通过 HTTP GET 发送内容的可能性。我在 spring 论坛上提到了一些讨论,但没有得到回答。 (请注意通过 http Post 发送数据工作正常)。讨论here建议改用 POST。
开发环境 - JBoss AS 5.1,Spring 3.1.3
客户端
@Test
public void testGetWithBody()
{
// acceptable media type
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.TEXT_PLAIN);
// header
HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);
// body
String body = "hello world";
HttpEntity<String> entity = new HttpEntity<String>(body, headers);
Map<String, Object> uriVariables = new HashMap<String, Object>();
uriVariables.put("id", "testFile");
// Send the request as GET
ResponseEntity<String> result = restTemplate.exchange(
"http://localhost:8080/WebApp/test/{id}/body",
HttpMethod.GET, entity, String.class, uriVariables);
Assert.assertNotNull(result.getBody());
}
服务器 @Controller
@RequestMapping(value = "/{id}/body", method = RequestMethod.GET)
public @ResponseBody
String testGetWithBody(@PathVariable String id,
@RequestBody String bodyContent)
{
return id + bodyContent;
}
问题 - 执行此测试用例会返回 500 Internal Server Error。在调试时,我发现 Controller 没有被击中。
RestTemplate 提供了将数据作为request body 发送的方式,但是由于服务器无法处理request body 导致错误的理解是否正确?
如果通过 HTTP Get 发送的请求体不是常规的,为什么 RestTemplate 提供允许发送它的 API?这是否意味着很少有服务器能够通过 GET 处理请求主体?
Resources - discussions on sending body via HTTP GET using RestTemplate at spring forum
Resources - General discussions on sending body via HTTP GET
is-this-statement-correct-http-get-method-always-has-no-message-body