4

Imagine that I have a Spring MVC controller something like this:

@Controller
@RequestMapping("/base-url")
public class MyController{

    //..snip
    @RequestMapping(method=RequestMethod.GET, value="/edit/{id}")
    public String edit(Model model, HttpServletRequest request, Authentication authentication){
        //..snip
    }
}

My question is regarding the inner value parameter to the @RequestMapping annotation at the function level. Is the pre-slash on /edit/{id} required, or does edit/{id} do the job just as well? I would have imagined that the pre-slash would set the request to be absolute, regardless of the class level mapping, but it seems to be ignored.

Is one or the other considered better practice? In the Spring documentation, they seem to always use the pre-slash. Are there any practical benefits to doing that?

Thanks,

idb.

idbentley
  • 4,188
  • 3
  • 34
  • 50

2 Answers2

4

According to the spring documentation, having a class level @RequestMapping annotation implies that all method level @RequestMappings will be relative to that of the class'.

It might be nice however, to have the ability to override the relative mappings in some rare cases.

okchan
  • 91
  • 3
0

I personally prefer to add pre-slash in value of @RequestMapping. In code level you can see: If the value does not start with an / then Spring (DefaultAnnotationHandlerMapping) will add it. Details answer you can visit: Use or not leading slash in value for @RequestMapping. Need official docs or point to Spring source?

Community
  • 1
  • 1
shohan
  • 303
  • 3
  • 16