我正在尝试将CQRS原则应用于具有域驱动设计原则的REST API,使用媒体类型的5个级别,如以下文章中所述:https://www.infoq.com/articles/rest-api-on-cqrs http://byterot.blogspot.ch/2012/12/5-levels-of-media-type-rest-csds.html
我的技术背景是Spring REST framework版本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不允许我将相同的uri映射到不同的域模型媒体类型。你知道我怎么才能做到这点吗?我是不是遗漏了什么?
非常感谢:o)
发布于 2017-04-11 23:29:41
这是因为内容类型仍然是相同的application/json。请看一下Content-Type,您作为domain-model=CommandOne传递的只是一个参数,而syntax不会识别调用不同方法的区别。
这在answer Does HTTP content negotiation respect media type parameters上有更详细的描述
这是作为BUG提交给Spring团队的,但他们以"Work as designed“结束。
不幸的是,Spring目前不能处理这种情况。
https://stackoverflow.com/questions/43347396
复制相似问题