Announcement Announcement Module
Collapse
No announcement yet.
RestTemplate getForObject method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • RestTemplate getForObject method

    I have a very simple example here taken directly from the javadocs for Spring 3.0 M2 RestTemplate http://static.springsource.org/sprin...tTemplate.html


    Code:
    import org.springframework.web.client.RestTemplate;
    import java.util.*;
    import java.net.*;
    
    
    public class RestClient
    {
            public static void main(String args[])
                    throws Exception
            {
                    RestTemplate restTemplate = new RestTemplate();
                    Map<String, String> vars = Collections.singletonMap("hotel", "42");
                    String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/rooms/{hotel}", String.class, vars);
    
                    System.out.println("result = " + result);
            }
    }

    Obviously I expect a error response (404 Not Found)
    However, when I ran a tcpdump to capture the GET request I was surprised to see Wireshark recording the request as a POST

    tcpdump -s 1500 -n -i eth0 -w t.pcap port 80


    No. Time Source Destination Protocol Info
    4 0.144553 xxMASKEDxx xxMASKEDxx HTTP POST /hotels/42/rooms/42 HTTP/1.1

    Frame 4 (269 bytes on wire, 269 bytes captured)
    Ethernet II, Src: xxMASKEDxx, Dst: xxMASKEDxx
    Internet Protocol, Src: xxMASKEDxx, Dst: 208.77.188.166 (208.77.188.166)
    Transmission Control Protocol, Src Port: 50584 (50584), Dst Port: http (80), Seq: 1, Ack: 1, Len: 203
    Hypertext Transfer Protocol
    POST /hotels/42/rooms/42 HTTP/1.1\r\n
    Accept: text/plain, text/*\r\n
    User-Agent: Java/1.5.0_14\r\n
    Host: example.com\r\n
    Connection: keep-alive\r\n
    Content-type: application/x-www-form-urlencoded\r\n
    Content-Length: 0
    \r\n

    No. Time Source Destination Protocol Info
    8 0.263067 208.77.188.166 xxMASKEDxx HTTP [TCP Out-Of-Order] HTTP/1.1 404 Not Found (text/html)

    Frame 8 (537 bytes on wire, 537 bytes captured)
    Ethernet II, Src: xxMASKEDxx, Dst: xxMASKEDxx
    Internet Protocol, Src: 208.77.188.166 (208.77.188.166), Dst: xxMASKEDxx
    Transmission Control Protocol, Src Port: http (80), Dst Port: 50584 (50584), Seq: 1, Ack: 204, Len: 471
    Hypertext Transfer Protocol
    HTTP/1.1 404 Not Found\r\n
    Date: Sun, 08 Mar 2009 23:06:33 GMT\r\n
    Server: Apache/2.2.3 (CentOS)\r\n
    Content-Length: 292
    Connection: close\r\n
    Content-Type: text/html; charset=iso-8859-1\r\n
    \r\n
    Line-based text data: text/html


    Am I missing something here? Shouldn't I be seeing an HTTP GET request?

    Thanks
    Y

  • #2
    See http://jira.springframework.org/browse/SPR-5536, which has been fixed.

    Comment


    • #3
      Works. Thanks

      Comment


      • #4
        thanks it's working for me

        Comment


        • #5
          newbie trying out the restTemplate

          Not able to get the sample code to work. RestTemplate initiation is expecting a multivaluemap.

          Comment


          • #6
            Originally posted by sdk22 View Post
            Not able to get the sample code to work. RestTemplate initiation is expecting a multivaluemap.
            figured it out . The classic case of class loading.

            Comment

            Working...
            X