Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:


1. Overview

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.

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.

2. Overriding the Favicon

The simplest way to override the default favicon of a Spring Boot application is to place the new favicon in the resources directory:


The favicon file should have the “favicon.ico” name.

We may also put that file in the static directory inside the project’s resources directory:


Spring Boot while starting up, scans for the favicon.ico file in the root resources location followed by static content locations.

3. Using a Custom Location

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.

We can do that by disabling the default favicon in our file:


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.

And then implementing our handler:

public class FaviconConfiguration {
    public SimpleUrlHandlerMapping customFaviconHandlerMapping() {
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
          "/favicon.ico", faviconRequestHandler()));
        return mapping;

    protected ResourceHttpRequestHandler faviconRequestHandler() {
        ResourceHttpRequestHandler requestHandler
          = new ResourceHttpRequestHandler();
        ClassPathResource classPathResource 
          = new ClassPathResource("com/baeldung/images/");
        List<Resource> locations = Arrays.asList(classPathResource);
        return requestHandler;

Notice that we have set Integer.MIN_VALUE for the mapping order, so give this handler the highest priority.

With this configuration, we can store our favicon file at any location within the application structure.

4. Gracefully Disable 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.

We can avoid this with a custom favicon controller, that returns an empty response:


static class FaviconController {
    void returnNoFavicon() {


5. Conclusion

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.

As always the code samples are available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

res – REST with Spring (eBook) (everywhere)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.