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


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


    where is user's emailId.

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


    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/, then emailId variable will be populated by value 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

Sign up using

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: