Spring RestTemplate - 带有请求正文的 http GET [英] Spring RestTemplate - http GET with request body

查看:34
本文介绍了Spring RestTemplate - 带有请求正文的 http GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
带有请求正文的 HTTP GET

我在这里读过一些不提倡通过 HTTP GET 发送内容的讨论.可以通过客户端(Web 浏览器)发送的数据大小有限制.处理 GET 数据也取决于服务器.请参阅下面的资源部分.

但是,我被要求测试使用 RestTemplate 通过 HTTP GET 发送内容的可能性.我在 spring 论坛上提到了一些讨论,但没有得到答复.(请注意通过 http Post 发送数据工作正常).此处的讨论建议改用 POST.

开发环境 - JBoss AS 5.1,Spring 3.1.3

客户

 @Testpublic void testGetWithBody(){//可接受的媒体类型列表可接受的媒体类型 = 新的 ArrayList();可接受的MediaTypes.add(MediaType.TEXT_PLAIN);//标题HttpHeaders headers = new HttpHeaders();headers.setAccept(acceptableMediaTypes);//身体String body = "你好世界";HttpEntityentity = new HttpEntity(body, headers);映射<字符串,对象>uriVariables = new HashMap();uriVariables.put("id", "testFile");//以 GET 方式发送请求ResponseEntity结果 = 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)公共@ResponseBodyString testGetWithBody(@PathVariable String id,@RequestBody 字符串 bodyContent){返回 id + bodyContent;}

问题 -执行此测试用例返回 500 内部服务器错误.调试的时候发现控制器没有命中.

  1. RestTemplate 提供了将数据作为请求正文发送的方式,但由于服务器无法处理请求正文而发生错误的理解是否正确?

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

<块引用>

资源 - 在 spring 论坛上讨论使用 RestTemplate 通过 HTTP GET 发送正文

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<块引用>

资源 - 关于通过 HTTP GET 发送正文的一般讨论

get-with-request-body

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

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

http-uri-get-limit

解决方案

这样理解是否正确,RestTemplate 提供了将数据作为请求体发送的方式,但由于服务器无法处理请求体而发生错误?

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

<块引用>

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

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

带有请求正文的HTTP GET所述:

<块引用>

换句话说,任何 HTTP 请求消息都允许包含消息体,因此 [a server] 必须考虑到这一点来解析消息.但是,GET 的服务器语义受到限制,因此主体(如果有)对请求没有语义意义.解析的需求和方法语义的需求是分开的.

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

<块引用>

但是,我被要求测试通过 HTTP GET 发送内容的可能性

请告诉提出此要求的人,这是一个不应进行测试的案例,因为没有合理的实现支持它.

Possible Duplicate:
HTTP GET with request body

I've read few discussions here which do not advocate sending content via HTTP GET. There are restrictions on the size of data that can be sent via clients (web browsers). And handling GET data also depends on servers. Please refer section Resources below.

However, I've been asked to test the possibility to send content via HTTP GET using RestTemplate. I refered few discussions on spring forum but they were not answered. (Please note sending data via http Post works fine). The discussion here suggests using POST instead.

dev env - 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 @Controller

    @RequestMapping(value = "/{id}/body", method = RequestMethod.GET)
public @ResponseBody
String testGetWithBody(@PathVariable String id,
        @RequestBody String bodyContent)
{
    return id + bodyContent;
}

The problem - executing this test case returns 500 Internal Server Error. On debugging, I found that the controller is not hit.

  1. 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 ?

  2. 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 ?

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 ?

You can tell by looking at network traffic (does the request get sent with a request body and a GET method?) and at server logs (the 500 result you receive must have a server-side effect that gets logged, and if not, configure the server to do so).

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 ?

Because it is a generic class that also allows you to craft requests that can include a message body.

As stated in 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.

A body on a GET cannot do anything semantically, because you are requesting a resource. It's like you tell the server: "Give me resource X, oh, and have some apples!". The server won't care about your apples and happily serve resource X - or throw an error because it doesn't like any offers in a request.

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

Please tell the one who requested this that this is a case that should not have to be tested, because no sensible implementation supports it.

这篇关于Spring RestTemplate - 带有请求正文的 http GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆