缺少字符串类型的方法参数的URI模板变量'id'-Spring MVC

时间:2019-12-08 12:15:06

标签: spring-mvc request-mapping

我使用以下代码构建了Spring MVC控制器:

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

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

        (snip ...)
    }

当我使用以下URL调用控制器时:http://myurl/edit-soldier/Q65683623

我收到以下错误:

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

我试图用值或路径替换名称,但是它也不起作用(这次我收到404错误)。

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

更改此

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

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

注意:将名称更改为

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