I am trying to come with a good convention to do request mappings in my application
right now i have
RegistrationController {
@RequestMapping(value="/registerMerchant")
...
@RequestMapping(value="/registerUser")
...
}
but this isnt ideal since by looking at the url you might not know to look in RegistrationController
for the code.
Is there a way i can programmitically prepend the controller name of those mappings making them:
/registration/registerMerchant
/registration/registerUser
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. Today we will look into various usage of this annotation with example and other annotations @PathVariable and @RequestParam .
@RequestMapping is used at the class level while @GetMapping is used to connect the methods. This is also an important Spring MVC interview question to knowing how and when to use both RequestMapping and GetMapping is crucial for Java developers.
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.
Specifically, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod. GET) . @GetMapping supports the consumes attribute like @RequestMapping .
Not programmatically, but this sort of pattern I've seen working:
@Controller
@RequestMapping(value="/registration/**")
RegistrationController {
@RequestMapping(value="**/registerMerchant")
...
@RequestMapping(value="**/registerUser")
...
}
Having said that, in the past I've found this inordinately hard to get working in the way I'd expect. It can be made to work, though.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With