Adding custom RequestCondition’s in Spring mvc 3.1

Spring MVC already provides a mechanism for distinguishing between json and html, the RequestMapping annotation takes a consumes attribute which looks at the content type of the request…

// REST version, Content-type is "application/json"
@RequestMapping(value = "https://stackoverflow.com/", consumes = "application/json")
public void myRestService() {
...

// HTML version, Content-type is not "application/json"
@RequestMapping(value = "https://stackoverflow.com/", consumes = "!application/json")
public void myHtmlService() {
...

Another way to use the same url but have distinct methods is with the param or headers attribute…

// the url is /?role=guest
@RequestMapping(value = "https://stackoverflow.com/", param = "role=guest")
public void guestService() {

// the url is / with header role=admin
@RequestMapping(value = "https://stackoverflow.com/", headers = "role=admin")
public void adminService() {

I would think you would want distinct urls for security. Typically, with something like Spring Security, you would put all of the admin functionality under /admin and let the framework manage it all…

<http auto-config="true">
  <intercept-url pattern="/admin/**" access="ROLE_ADMIN" />
...

Would this be sufficient for your use case(s)?

Leave a Comment