2010-06-03 47 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負責將 消息轉換爲對象。