SpringBoot : How to bind data from url to a variable

Binding data from URL to a variable

Suppose you want to have bind some data from URL then you can do the following

  • create the @RequestMapping annotation e.g
    • /list/{id}
    • /{version}/list/{id}
  • bind the Url parameters using @PathVariable annotation e.g
    • @PathVariable("id") Long id
    • @PathVariable("version") int version,@PathVariable("id") Long id

Complete code may look like this,

    @RequestMapping("list/{id}")
    public Object list(@PathVariable("id") Long id ){

     // Logic goes here 

    }


    @RequestMapping("/{version}/list/{id}")
    public Object list(@PathVariable("version") int version,@PathVariable("id") Long id ){

     // Logic goes here 

    }

Leave a Comment

Powered by Blogger.