Announcement Announcement Module
Collapse
No announcement yet.
RequestMethod.GET Method Not Working with HttpEntity Argument Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • RequestMethod.GET Method Not Working with HttpEntity Argument

    I'm developing a RESTful web service with the Spring 3 MVC approach. I have the following method defined in my controller:

    @RequestMapping(method=RequestMapping.GET,
    value="/getXXXXYYYY/person/{identifier}")
    public ResponseEntity<CustomReturnClass> getXXXXYYYY(
    @PathVariable String identifier,
    HttpEntity<CustomPersonClass> customPersonClass)
    {
    System.out.println("Inside of getXXXXYYYY method");
    ...
    }


    I wrote the following client code to call / test the above method:

    ResponseEntity<CustomReturnClass> response = null;
    CustomPersonClass customPersonClass = new CustomPersonClass();
    customerPersonClass.setAttribute1("stringValue");
    HttpEntity<CustomPersonClass> requestEntity =
    new HttpEntity<CustomPersonClass>(customPersonClass);
    String identifier = "identifierString";

    response = restTemplate.exchange(
    BASE_URL+"/getXXXXYYYY/person/{identifier}",
    HttpMethod.GET,
    requestEntity,
    CustomReturnClass.class,
    identifier);


    However when I run this client code I get the following error on the server side:

    ERROR [[Catalina].[localhost].[/webAppName].[webAppName]] Servlet.service() for servlet webAppName threw Exception
    java.lang.NullPointerException
    at com.sun.xml.bind.v2.runtime.unmarshaller.Unmarshal lingContext.getLocation(UnmarshallingContext.java: 560)
    at com.sun.xml.bind.v2.runtime.Coordinator.propagateE vent(Coordinator.java:153)
    at com.sun.xml.bind.v2.runtime.Coordinator.fatalError (Coordinator.java:147)
    ...


    If I change the controller method signature to look like this:

    @RequestMapping(method=RequestMapping.POST,
    value="/getXXXXYYYY/person/{identifier}")
    public ResponseEntity<CustomReturnClass> getXXXXYYYY(
    @PathVariable String identifier,
    HttpEntity<CustomPersonClass> customPersonClass)
    {
    System.out.println("Inside of getXXXXYYYY method");
    ...
    }


    and the client to call the method like this:

    ResponseEntity<CustomReturnClass> response = null;
    CustomPersonClass customPersonClass = new CustomPersonClass();
    customerPersonClass.setAttribute1("stringValue");
    HttpEntity<CustomPersonClass> requestEntity =
    new HttpEntity<CustomPersonClass>(customPersonClass);
    String identifier = "identifierString";

    response = restTemplate.exchange(
    BASE_URL+"/getXXXXYYYY/person/{identifier}",
    HttpMethod.POST,
    requestEntity,
    CustomReturnClass.class,
    identifier);


    I get the "Inside of getXXXXYYYY method" printed out to the server the controller is deployed to. It also works if I change the controller method signature / caller to use a PUT.

    Is the HttpEntity argument not supported for GET? I have seen the following example from the spring documentation (http://static.springsource.org/sprin.../remoting.html), in section 19.9.1.1 Dealing with request and response headers:

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.set("MyRequestHeader", "MyValue");
    HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);

    HttpEntity<String> response = template.exchange("http://example.com/hotels/{hotel}",
    HttpMethod.GET, requestEntity, String.class, "42");

    String responseHeader = response.getHeaders().getFirst("MyResponseHeader") ;
    String body = response.getBody();


    This example shows the HttpMethod.GET call with a HttpEntity argument being passed in.

    Am I doing something wrong here? Again can a method on a controller be of type RequestMethod.GET and utilize and HttpEntity argument?

  • #2
    From an HTTP point of view, this would mean sending a GET request with a request body. I wouldn't recommend trying that, although it's technically possible. But it *will* confuse lots of components.

    (Sending headers with GET of course is ok)

    Comment


    • #3
      So is there any recommended way to pass in a custom object as an argument for a GET controller method? The functionality of the method I originally posted uses data out of the argument CustomPersonClass in order to retrieve data from a database. To me, this fits in line with a GET request.

      Comment

      Working...
      X