1

I am trying to apply CQRS principles on my REST API with domain-driven-design principles, using the 5 levels of Media Types, as explained in these articles: https://www.infoq.com/articles/rest-api-on-cqrs http://byterot.blogspot.ch/2012/12/5-levels-of-media-type-rest-csds.html

My technical context is Spring REST framework version 3.2.

Basically, i need to be able to map my commands using different "domain-model" media types. Therefore, i would expect the following mapping to work:

@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);
    }

}

Problem is, I am getting mapping errors when requesting for a PUT:

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

Error is:

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

Spring doesn't allow me to map the same uri the different domain-model Media Types. Any idea how could I achieve that? Am I missing something?

Many thanks :o)

Constantin Galbenu
  • 16,951
  • 3
  • 38
  • 54

1 Answers1

1

That's because the content-type is still the same application/json. Please look at Content-Type syntax What you are passing as domain-model=CommandOne is just a parameter and Spring doesn't recognize as a difference to call the different methods.

This is described in more detail on answer Does HTTP content negotiation respect media type parameters

This was submitted as a BUG to the Spring team but they closed with "Work as designed".

Unfortunately Spring can't treat this case currently.

Community
  • 1
  • 1
Bruno Leite
  • 542
  • 4
  • 12