Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring mvc request mapping conventions

Tags:

spring-mvc

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
like image 631
mkoryak Avatar asked Jan 12 '11 16:01

mkoryak


People also ask

What is @RequestMapping annotation in Spring?

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 .

What is @RequestMapping and GetMapping?

@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.

What is true about @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.

How can @GetMapping annotation be written by using @RequestMapping method?

Specifically, @GetMapping is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod. GET) . @GetMapping supports the consumes attribute like @RequestMapping .


1 Answers

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.

like image 194
skaffman Avatar answered Sep 20 '22 16:09

skaffman