如何避免Spring Boot Controller捕获favicon请求



我有以下控制器

@Controller
public class MyController {
@GetMapping({"/", "/{name}"})
public String hello(@PathVariable(required = false) String name) {
System.out.println("Name: " + name);
return "hello";
}
}

和这个目录结构

- resources
- static
- favicon.ico
- templates
- hello.html

当我通过浏览器GEThttp://localhost:8080/时,我的应用程序会打印:

Name: null
Name: favicon.ico

所以/favicon.ico的请求被我的控制器捕获了,但我希望Spring处理这个请求,并返回放置在这里的favicon.icoresources/static/favicon.ico

我认为应该有一种不添加几行配置的方法。

您可以采取的一种方法是通过以下属性禁用收藏夹解析:

spring.mvc.favicon.enabled=false

我不得不在我的html头中包含<link rel="icon" type="image/x-icon" href="images/favicon.ico">,并且它起了作用。images前缀不是必需的,但我将图标移到了它们中,因为我允许向/images发出任何未经验证的请求,所以我不需要favicon.ico的额外规则。这是我目前的目录结构:

- resources
- static
- images
- favicon.ico
- templates
- hello.html

感谢Ajay Kumar的帮助。

最新更新