2010-06-03 66 views
4

我找不出来几个参数和标题绑定到一个请求参数使用注释在春季3绑定多个请求参数一个对象在春季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) { 
... 
} 

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

在此先感谢。

回答

2

@RequestBody是你想要的,我想。请参阅Spring文档here

@RequestBody方法参数 注释指示的方法 参数应绑定到HTTP请求正文的值 。

通过使用 HttpMessageConverter将请求主体转换为 方法参数。 HttpMessageConverter负责将 消息转换为对象。