开发者

Bind multiple request parameters to one object in Spring 3

开发者 https://www.devze.com 2023-01-02 07:05 出处:网络
I can\'t figure out a way to bind several arguments and headers to one request parameter using annotations in Spring 3.

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 t开发者_如何学运维o achieve something similar to what I need?

Thanks in advance.


@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.

0

精彩评论

暂无评论...
验证码 换一张
取 消