java – 在Spring 3中将多个请求参数绑定到一个对象

我无法想出一种方法,可以使用
Spring 3中的注释将多个参数和标头绑定到一个请求参数.

例如,假设我收到了这个请求:

Headers:
Content-type: text/plain;

POST Body:
Name: Max

现在我想要一切都神秘地绑定到这个对象:

class NameInfo {
    String name;
}

使用这样的代码:

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

所以最终我可以使用:

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

有没有办法实现类似于我需要的东西?

提前致谢.

最佳答案 我认为@RequestBody是你想要的.请参阅有关它的Spring文档
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.

点赞