Search code examples
spring-bootannotations

Springboot @PathVariable value is including parenthesis


I am creating simple SpringBoot application. I declared PUT operation as follows

@RequestMapping(method=RequestMethod.PUT, value= "/topics/{id}")
public void updateTopic(@PathVariable String id, Topic t){
    System.out.println("Kaushik==="+id);
    topicService.updateTopic(Integer.parseInt(id),t);
}

When I invoke PUT operation on URL http://localhost:8080/topics/{2}. It fails. The value of variable id is "{2}" instead of simply "2" which is causing number format exception.

I also tried specifying parameter name. public void updateTopic(@PathVariable(name="id") String id, Topic t){ but ut did not work either.


Solution

  • Your decratation of the PUT endpoint is perfectly fine:

    @RequestMapping(method=RequestMethod.PUT, value= "/topics/{id}")
    

    To call the URL simply use the desired value in place of {id}:

    http://localhost:8080/topics/2
    

    The URL template variable {var} is just an expression which marks part of the URL to be converted into the method parameter.