2017-04-11 15 views
1

我想在我的REST API和領域驅動設計原則也適用CQRS的原則,採用5級媒體類型,在這些文章中解釋說: https://www.infoq.com/articles/rest-api-on-cqrs http://byterot.blogspot.ch/2012/12/5-levels-of-media-type-rest-csds.html5級媒體類型的彈簧安置

我的技術環境是Spring REST框架版本3.2。

基本上,我需要能夠映射我的命令使用不同的「域模型」媒體類型。 因此,我希望下面的測繪工作:

@Controller 
@RequestMapping("resources") 
public class MyController { 

    @RequestMapping(value = "{id}", method = RequestMethod.PUT, consumes = "application/json;domain-model=CommandOne") 
    @ResponseBody 
    public void commandOne(@PathVariable Long id, @RequestBody CommandOne commandOne) { 
     LOG.info("Using command {}", commandOne); 
    } 

    @RequestMapping(value = "{id}", method = RequestMethod.PUT, consumes = "application/json;domain-model=CommandTwo") 
    @ResponseBody 
    public void commandTwo(@PathVariable Long id, @RequestBody CommandTwo commandTwo) { 
     LOG.info("Using command {}", commandTwo); 
    } 

} 

問題是請求一個PUT時,我得到映射錯誤:

PUT /resources/123 
Content-Type: application/json;domain-model=CommandOne 

錯誤是:

java.lang.IllegalStateException: Ambiguous handler methods mapped for HTTP path ... 

Spring不允許我映射不同的域模型媒體類型。任何想法我怎麼能做到這一點?我錯過了什麼嗎?

非常感謝 :O)

回答

0

這是因爲內容類型仍然是相同的application/json。請看內容類型syntax 您傳遞的內容爲domain-model=CommandOne僅僅是一個參數,Spring並不認爲調用不同方法的區別。

這更詳細的答案 Does HTTP content negotiation respect media type parameters

這是作爲一份BUG到春節的球隊,但他們與關閉「工作作爲設計的」描述。

不幸的是,Spring目前無法處理這種情況。