Search code examples
spring-mvcrequest-mapping

Missing URI template variable 'id' for method parameter of type String - Spring MVC


I have built a Spring MVC controller using the following code:

@RequestMapping(name = "/edit-soldier/{id}", method = RequestMethod.GET)
    public ModelAndView editSoldierForm(@PathVariable String id) throws FileNotFoundException {

        System.out.println("id:" + id);

        (snip ...)
    }

When I call the controller using the following URL : http://myurl/edit-soldier/Q65683623,

I get the following error:

There was an unexpected error (type=Internal Server Error, status=500).
Missing URI template variable 'id' for method parameter of type String
org.springframework.web.bind.MissingPathVariableException: Missing URI template variable 'id' for method parameter of type String

I have tried to replace name by value or path but it doesn't work either (I am getting a 404 error this time).

What am I doing wrong?


Solution

  • Change this

    @RequestMapping(name = "/edit-soldier/{id}", method = RequestMethod.GET)
    

    to

    @RequestMapping(value = "/edit-soldier/{id}", method = RequestMethod.GET)
    

    Note: Change name to value

    or

    @GetMapping("/edit-soldier/{id}")