传递url变量会导致“找不到HTTP请求的映射”

时间:2018-07-05 15:20:17

标签: java spring

我能够编写以下方法来很好地加载我的静态文件:

@GetMapping("/register")
public String newRegister(HttpServletRequest request, Model model) {
    return "register";
}

但是,当我在网址中添加{Id}

@GetMapping("/register/{Id}")
public String newRegister(HttpServletRequest request, Model model) {
    return "register";
}

所有静态文件错误,并显示以下消息:

2018-07-05 08:42:24.969  WARN 52397 --- [nio-8080-exec-4] o.s.web.servlet.PageNotFound             :     
No mapping found for HTTP request with 
URI [/hi/js/all.js] in DispatcherServlet with name 'dispatcherServlet'
2018-07-05 08:42:24.969  WARN 52397 --- [nio-8080-exec-3] o.s.web.servlet.PageNotFound          
 : No mapping found for HTTP request with 
URI [/hi/img/google-logo.svg] in DispatcherServlet with name 'dispatcherServlet'
 2018-07-05 08:42:25 DEBUG GreetingController:40 - *****123
 2018-07-05 08:42:25.563  WARN 52397 --- [nio-8080-exec-7] o.s.web.servlet.PageNotFound             : No mapping found for HTTP request with 
URI [/hi/css/all.css] in DispatcherServlet with name 'dispatcherServlet'

为什么会发生这种情况,我该如何解决?这是项目结构的示例:

enter image description here

1 个答案:

答案 0 :(得分:0)

鉴于您已向请求中添加了路径变量-@GetMapping("/register/{Id}") public String newRegister(HttpServletRequest request, Model model, @PathVariable("Id") long id) { return "register"; } ,该方法需要@PathVariable参数。所以这就像

3 ^ 10

希望这会有所帮助。