可能的重复:
带请求正文的 HTTP GET https://stackoverflow.com/questions/978061/http-get-with-request-body
我在这里读过一些不提倡通过 HTTP GET 发送内容的讨论。通过客户端(Web 浏览器)发送的数据大小有限制。并且处理GET数据也依赖于服务器。请参阅部分资源以下。
但是,我被要求测试使用 RestTemplate 通过 HTTP GET 发送内容的可能性。我在春季论坛上提到了一些讨论,但没有得到答复。 (请注意,通过 http Post 发送数据效果很好)。讨论here https://stackoverflow.com/questions/11091160/rest-api-get-request-with-body建议使用 POST 代替。
开发环境 - JBoss AS 5.1,Spring 3.1.3
Client
@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());
}
Server@控制器
@RequestMapping(value = "/{id}/body", method = RequestMethod.GET)
public @ResponseBody
String testGetWithBody(@PathVariable String id,
@RequestBody String bodyContent)
{
return id + bodyContent;
}
问题-
执行此测试用例会返回 500 内部服务器错误。在调试时,我发现控制器没有被命中。
理解RestTemplate提供了将数据作为请求体发送的方式,但由于服务器无法处理请求体而发生错误,是否正确?
如果通过 HTTP Get 发送的请求正文不是常规的,为什么 RestTemplate 提供允许发送它的 API?这是否意味着很少有服务器能够通过 GET 处理请求正文?
资源- Spring 论坛上关于使用 RestTemplate 通过 HTTP GET 发送正文的讨论
资源- 关于通过 HTTP GET 发送正文的一般讨论
获取请求主体 https://stackoverflow.com/questions/978061/http-get-with-request-body
is-this-statement- Correct-http-get-method-always-has-no-message-body https://stackoverflow.com/questions/5216567/is-this-statement-correct-http-get-method-always-has-no-message-body
读取请求正文时获取或发布 https://stackoverflow.com/questions/6031136/get-or-post-when-reading-request-body
http-uri-获取限制 https://stackoverflow.com/questions/266322/http-uri-get-limit