Spring RestTemplate - 带请求主体的http GET [英] Spring RestTemplate - http GET with request body

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

问题描述


可能存在重复:

HTTP GET with request body


我读过很少这里的讨论不主张通过HTTP GET发送内容。可以通过客户端(网络浏览器)发送的数据大小有限制。处理GET数据也取决于服务器。请参阅下面的资源部分。 然而,我被要求测试使用RestTemplate通过HTTP GET发送内容的可能性。我在春季论坛上讨论了一些讨论,但他们没有回答。 (请注意通过http Post发送数据正常工作)。 此处的讨论建议使用POST。



开发环境 - JBoss AS 5.1,Spring 3.1.3



客户

$ $ p $ @Test
public void testGetWithBody()
{
//可接受的媒体类型
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);

地图< String,Object> uriVariables = new HashMap< String,Object>();
uriVariables.put(id,testFile);

//以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内部服务器错误。在调试时,我发现控制器没有被击中。


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

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


    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

    是,这个语句纠正-HTTP-GET-方法,始终有 - 无消息-body

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

    http-uri-get-limit

    解决方案


    理解RestTemplate提供发送数据的方式是正确的,但是发生错误是因为服务器无法处理请求体?


    通过查看网络流量(请求是否与请求主体和GET方法一起发送?)和服务器日志(您收到的500结果必须有服务器端e如果通过HTTP Get发送的请求主体不是常规的,那么可以通过配置服务器来完成这项工作。


    为什么RestTemplate提供API以允许发送它?这是否意味着很少有服务器可以通过GET来处理请求体?


    因为它是一个通用类,它也允许你可以包括消息正文。



    正如 HTTP GET与请求正文


    换句话说,任何HTTP请求消息都被允许包含一个消息体,因此[服务器]必须解析消息。但是,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天全站免登陆