1. Overview
1.概述
A favicon is a small website icon displayed in a browser, usually next to an address.
图标是一个显示在浏览器中的小网站图标,通常在地址旁边。
Often we don’t want to settle for the default ones provided by various frameworks such a Spring Boot.
通常情况下,我们不想满足于各种框架(如Spring Boot)所提供的默认的框架。
In this quick tutorial, we’ll discuss how to customize the favicon of a Spring Boot application, by looking into various approaches to customizing the favicon.
在这个快速教程中,我们将讨论如何定制Spring Boot应用程序的favicon,通过研究定制favicon的各种方法来实现。
2. Overriding the Favicon
2.重写Favicon
The simplest way to override the default favicon of a Spring Boot application is to place the new favicon in the resources directory:
覆盖Spring Boot应用程序的默认favicon的最简单方法是:将新的favicon放在resources目录下。
src/main/resources/favicon.ico
The favicon file should have the “favicon.ico” name.
favicon文件应该有”favicon.ico”名称。
We may also put that file in the static directory inside the project’s resources directory:
我们也可以将该文件放在项目资源目录内的static目录下。
src/main/resources/static/favicon.ico
Spring Boot while starting up, scans for the favicon.ico file in the root resources location followed by static content locations.
Spring Boot在启动时,会扫描根资源位置的favicon.ico文件,然后是静态内容位置。
3. Using a Custom Location
3.使用自定义位置
Instead of putting the favicon in the root level of the resources directory, we might want to keep it along with other images of the application.
与其把favicon放在资源目录的根层,我们可能想把它和应用程序的其他图像放在一起。
We can do that by disabling the default favicon in our application.properties file:
我们可以通过在application.properties文件中禁用默认favicon来做到这一点。
spring.mvc.favicon.enabled=false
It’s worth mentioning that, as of Spring Boot 2.2, this configuration property is deprecated. Moreover, Spring Boot no longer provides a default favicon, as this icon can be classified as information leakage.
值得一提的是,从Spring Boot 2.2开始,这个配置属性已被废弃。此外,Spring Boot不再提供默认的favicon,因为这个图标可以被归为信息泄露。。
And then implementing our handler:
然后实现我们的处理程序。
@Configuration
public class FaviconConfiguration {
@Bean
public SimpleUrlHandlerMapping customFaviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(Integer.MIN_VALUE);
mapping.setUrlMap(Collections.singletonMap(
"/favicon.ico", faviconRequestHandler()));
return mapping;
}
@Bean
protected ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler
= new ResourceHttpRequestHandler();
ClassPathResource classPathResource
= new ClassPathResource("com/baeldung/images/");
List<Resource> locations = Arrays.asList(classPathResource);
requestHandler.setLocations(locations);
return requestHandler;
}
}
Notice that we have set Integer.MIN_VALUE for the mapping order, so give this handler the highest priority.
注意,我们已经为映射顺序设置了Integer.MIN_VALUE,所以给这个处理程序以最高的优先级。
With this configuration, we can store our favicon file at any location within the application structure.
有了这个配置,我们可以将我们的favicon文件存储在应用程序结构的任何位置。
4. Gracefully Disable Favicon
4.优雅地停用Favicon
If we don’t want any favicon for our application, we can disable it by setting the property spring.mvc.favicon.enabled to false. But with this when the browsers lookup they get a “404 Not Found” error.
如果我们不希望我们的应用程序有任何favicon,我们可以通过设置属性spring.mvc.favicon.enabled来禁用它。但这样一来,当浏览器查询时,他们会得到一个 “404 Not Found “的错误。
We can avoid this with a custom favicon controller, that returns an empty response:
我们可以用一个自定义的favicon控制器来避免这种情况,该控制器返回一个空的响应。
//...
@Controller
static class FaviconController {
@GetMapping("favicon.ico")
@ResponseBody
void returnNoFavicon() {
}
}
//...
5. Conclusion
5.总结
In this article, we saw, how to override the default favicon of a Spring boot application, use a custom location for the favicon and, how to avoid the 404 error if we do not want to use a favicon.
在这篇文章中,我们看到了如何覆盖Spring boot应用程序的默认favicon,为favicon使用一个自定义的位置,以及如果我们不想使用favicon,如何避免出现404错误。
As always the code samples are available over on GitHub.
像往常一样,代码样本可在GitHub上获得。。