Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Spring MVC @RequestParam vs @PathVariable

    • 0
    • 1
    • 0
    • 1
    • 1
    • 0
    • 0
    • 0
    • 13.7k
    Comment on it

    @PathVariable:

    Sometimes we need to pass parameters along with the url to get the data. In Spring MVC framework, we can customize the URL in order to get data. For this purpose @PathVariable annotation is used in Spring framework. @PathVariable is used to obtain some placeholder from the uri (Spring call it an URI Template).

    @RequestParam:

    @RequestParam is used to obtain an request parameter. @RequestParam binds a request parameter to a parameter in your method.

    For example you want to write a url to get user's details according to createDate as below

    http://localhost:8080/app/findMe/user@gmail.com?createDate=09-18-2015
    

    where user@gmail.com is user's emailId.

    Now we'll define this url in Spring MVC controller as below:

    /findMe/{emailId}
    

    We'll declare emailId as path variable and will define createdDate as request param:

    @RequestMapping(value = "/findMe/{emailId:.+}", method = RequestMethod.GET)
        public ModelAndView findMe(HttpServletRequest request,
                HttpServletResponse response,@PathVariable String emailId,@RequestParam(value = "createDate", required = false) Date createDate)
        {
    }
    

    As you can see in the above example "emailId" is a PathVariable and "createDate" is a RequestParam, so if we use url http://localhost:8080/app/findMe/user@gmail.com?createDate=09-18-2015, then emailId variable will be populated by value user@gmail.com and the value of the parameter named "createDate" will be passed as the "createDate" argument in the above method.

    Hope this will help you :)

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: