Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Guide to the Favicon in Spring Boot

$
0
0

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:

src/main/resources/favicon.ico

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

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

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.

3. Using a Custom Location

Instead of putting the favicon in the root level of 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 application.properties file:

spring.mvc.favicon.enabled=false

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("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.

With this configuration, we can store our favicon file at any location within 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:

//...

@Controller
static class FaviconController {
 
    @GetMapping("favicon.ico")
    @ResponseBody
    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.


Viewing all articles
Browse latest Browse all 4535

Trending Articles