views:

451

answers:

1

Hi,

I can't figure out a way to bind several arguments and headers to one request parameter using annotations in Spring 3.

For example, let's say I'm getting this request:

Headers:
Content-type: text/plain;

POST Body:
Name: Max

Now I want it all to mysteriously bind to this object:

class NameInfo {
    String name;
}

Using some code like this:

String getName() {
    if ("text/plain".equals(headers.get("content-type"))) {
        return body.get("name");
    } else if ("xml".equals(headers.get("content-type")) {
        return parseXml(body).get("name");
    } else ...
}

So that in the end I would be able to use:

@RequestMapping(method = RequestMethod.POST)
void processName(@RequestAttribute NameInfo name) {
...
}

Is there a way to achieve something similar to what I need?

Thanks in advance.

+1  A: 

@RequestBody is what you want, I think. See the Spring docs about it here.

The @RequestBody method parameter annotation indicates that a method parameter should be bound to the value of the HTTP request body.

You convert the request body to the method argument by using an HttpMessageConverter. HttpMessageConverter is responsible for converting from the HTTP request message to an object.

skaffman