Spring - Annotation Based Controller - RequestMapping based on query string

SpringAnnotations

Spring Problem Overview


In Spring annotation-based controller, is it possible to map different query strings using @RequestMapping to different methods?

For example

@RequestMapping("/test.html?day=monday")
public void writeMonday() {
}


@RequestMapping("/test.html?day=tuesday")
public void writeTuesday() {
}

Spring Solutions


Solution 1 - Spring

Yes, you can use the http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/web/bind/annotation/RequestMapping.html#params()">params</a> element:

@RequestMapping("/test.html", params = "day=monday")
public void writeMonday() {
}

@RequestMapping("/test.html", params = "day=tuesday")
public void writeTuesday() {
}

You can even map based on the presence or absence of a param:

@RequestMapping("/test.html", params = "day")
public void writeSomeDay() {
}

@RequestMapping("/test.html", params = "!day")
public void writeNoDay() {
}

Solution 2 - Spring

or you could do something like:

@RequestMapping("/test.html")
public void writeSomeDay(@RequestParam String day) {
   // code to handle "day" comes here...
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnjdeveloperView Question on Stackoverflow
Solution 1 - SpringHilton CampbellView Answer on Stackoverflow
Solution 2 - Springgu3stView Answer on Stackoverflow