http - Spring RestTemplate - 带有请求正文的 http GET

标签 http get

<分区>

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 没有被击中。

  1. RestTemplate 提供了将数据作为request body 发送的方式,但是由于服务器无法处理request body 导致错误的理解是否正确?

  2. 如果通过 HTTP Get 发送的请求体不是常规的,为什么 RestTemplate 提供允许发送它的 API?这是否意味着很少有服务器能够通过 GET 处理请求主体?

Resources - discussions on sending body via HTTP GET using RestTemplate at spring forum

http://forum.springsource.org/showthread.php?129510-Message-body-with-HTTP-GET&highlight=resttemplate+http+get

http://forum.springsource.org/showthread.php?94201-GET-method-on-RestTemplate-exchange-with-a-Body&highlight=resttemplate+http+get

Resources - General discussions on sending body via HTTP GET

get-with-request-body

is-this-statement-correct-http-get-method-always-has-no-message-body

get-or-post-when-reading-request-body

http-uri-get-limit

最佳答案

Is it correct to understand that the RestTemplate provides the way to send data as request body, but the error occurs because the server could not handle the request body ?

您可以通过查看网络流量(请求是否与请求正文和 GET 方法一起发送?)和服务器日志(您收到的 500 结果必须具有被记录的服务器端效果,如果不,配置服务器这样做)。

If the request body sent via HTTP Get is not conventional why does RestTemplate provide the APIs to allow sending it ? Does this mean there are few servers capable of handling the Request body via GET ?

因为它是一个通用类,还允许您制作包含消息正文的请求。

HTTP GET with request body 中所述:

In other words, any HTTP request message is allowed to contain a message body, and thus [a server] must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request. The requirements on parsing are separate from the requirements on method semantics.

GET 上的正文不能在语义上做任何事情,因为您正在请求资源。这就像你告诉服务器:“给我资源 X,哦,还有一些苹果!”。服务器不会关心您的苹果并愉快地提供资源 X - 或抛出错误,因为它不喜欢请求中的任何报价。

However, I've been asked to test the possibility to send content via HTTP GET

请告诉提出此要求的人,这是一个不必测试的案例,因为没有明智的实现支持它。

关于http - Spring RestTemplate - 带有请求正文的 http GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13270708/

相关文章:

http - windows下使用telnet测试HTTP

forms - 为什么基于表单的身份验证不被视为 RESTful?

http - Nginx 将 Http 重定向到 Https - 这里有什么问题?

http - 使用tomcat http服务共享tomcat日志

python - 使用 XML 响应处理 GET 请求

java - 不使用浏览器的 HTTP 请求

file - 使用psftp上传下载文件

java - 尝试获取 InputStream 时出现错误 502/504

javascript - Angularjs CMS 的 url 中的 GET 请求

javascript - $.get 路由未在服务器端调用,并且未返回任何数据,服务器未进行控制台日志记录