Hi Everyone - I have a question about the @RequestMapping "produces" attribute. I'm using Spring 3.2 and <mvc:annotation-driven>
The question is: if you have identical method signatures in separate classes/packages, but the "produces" attribute is the only difference, how will Spring decide to map the request if the Accept header is */*?
The reason I ask, is because I'd like to use the produces for versioning (produces="application/com.api-v1+json") an API so that requests get routed to the proper versions of methods.
From the testing I've done, it seems to call the whichever produces attribute value is alphabetically first. The problem, if that's the case is that "application/com.api-v10+json" would get called before "com.api-v9+json" and I would want it to always called the lowest version.
So again, from the classes below, if the request has the header Accept: */*, then it will invoke ControllerVersionOne.getSomething() - but my real question is: WHY?
ControllerVersionOne
ControllerVersionTwoCode:package com.package.name; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.package.name.model.ModelObject; @Controller public class ControllerVersionOne { @RequestMapping(method=RequestMethod.GET, value="/resourcename", produces="application/com.api-v1+json") @ResponseBody public ResponseEntity<ModelObject> getSomething() { ModelObject resObj = new ModelObject(); resObj.setVersion(1L); return new ResponseEntity<ModelObject>(resObj, HttpStatus.OK); } }
Code:package com.package.name.v2; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.package.name.model.ModelObject; @Controller public class ControllerVersionTwo { @RequestMapping(method=RequestMethod.GET, value="/resourcename", produces="application/com.api-v2+json") @ResponseBody public ResponseEntity<ModelObject> getSomething() { ModelObject resObj = new ModelObject(); resObj.setVersion(2L); return new ResponseEntity<ModelObject>(resObj, HttpStatus.OK); } }


Reply With Quote