Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring - Annotation Based Controller - RequestMapping based on query string

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() {
}
like image 863
njdeveloper Avatar asked Oct 16 '22 19:10

njdeveloper


People also ask

What is the use of @RequestMapping annotation?

RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.

Which of the following is the method where RequestMapping is used with RequestParam?

The @RequestParam annotation is used with @RequestMapping to bind a web request parameter to the parameter of the handler method.

Which of these can be the return type of a method annotated with @RequestMapping in Spring?

A Callable can be returned when the application wants to produce the return value asynchronously in a thread managed by Spring MVC. A DeferredResult can be returned when the application wants to produce the return value from a thread of its own choosing.

Which of these are valid usage of @RequestMapping annotation in Spring MVC?

The @RequestMapping annotation can be applied to class-level and/or method-level in a controller. The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.


1 Answers

Yes, you can use the params 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() {
}
like image 80
Hilton Campbell Avatar answered Oct 19 '22 09:10

Hilton Campbell