RequestMapping with multiple values with pathvariable - Spring 3.0

42,408

Ideally we can get pathvariable by using annotation @PathVariable in method argument but here you have used array of url {"/userDetails", "/userDetails/edit/{id}"} so this will give error while supply request like localhost:8080/domain_name/userDetails , in this case no id will be supplied to @PathVariable.

So you can get the difference (which request is comming through) by using argument HttpServletRequest request in method and use this request object as below -

String uri = request.getRequestURI();

Code is like this -

   @RequestMapping(value = {"/userDetails", "/userDetails/edit/{id}"}, method=RequestMethod.GET)
   public String userDetails(Map Model,HttpServletRequest request) {
   String uri = request.getRequestURI();  
  //put the condition based on uri
 }
Share:
42,408
Aravind Vel
Author by

Aravind Vel

Website Developer living in Chennai, India. Playing with HTML5, CSS3, JavaScript, jQuery, PHP, Drupal and now Drupal Gardens.

Updated on July 05, 2022

Comments

  • Aravind Vel
    Aravind Vel almost 2 years
    @RequestMapping(value = {"/userDetails", "/userDetails/edit/{id}"}, method = RequestMethod.GET)
    public String userDetails(Map Model,****) {
    //what goes here? 
    }
    

    What will be my arguments to the userDetails method? And how do I differentiate /userDetails and /userDetails/edit/9 within the method?