Custom Favicon in Spring Boot

In this article, we will discuss how to create a custom favicon in Spring Boot application.

 

Introduction

A favicon is a small, iconic image that represents your website. Favicons are most often found in the address bar of your web browser. Spring Boot looks for a favicon.ico in the predefined configured place. It also provides options to create a custom favicon for our Spring Boot application.

 

1. Override Favicon

Spring Boot looks for anfavicon.ico in the configured static content locations and the root of the classpath (in that order). If such a file is present, it is automatically used as the favicon of the application.We can create a custom favicon in Spring Boot application by placing new favicon either in the resource or static directory in our classpath.

Resource Directory

src/main/resources/favicon.ico

Static Directory

src/main/resources/static/favicon.ico

Make sure to name favicon file as “favicon.ico”

 

2. Custom Favicon Location

The option 1 is the best and recommended way to create a custom favicon for your Spring Boot application, however, Spring Boot provides an alternate option to completely disable auto scanning process and take complete control using a custom configuration. We can do that by disabling the default favicon in our application.properties file.

spring.mvc.favicon.enabled=false

We add a custom HandlerMapping to do it.

@Configuration
public class FaviconConfiguration {

 @Bean
 public SimpleUrlHandlerMapping faviconHandlerMapping() {
  SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
  mapping.setOrder(Integer.MIN_VALUE);
  mapping.setUrlMap(Collections.singletonMap("https://prod-acb5.kxcdn.com/favicon.ico",
   faviconRequestHandler()));
  return mapping;
 }

 @Bean
 protected ResourceHttpRequestHandler faviconRequestHandler() {
  ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
  requestHandler.setLocations(Arrays
   . < Resource > asList(new ClassPathResource("/")));
  return requestHandler;
 }

}

In the above code, you are free to refer the favicon file at any location.

 

3. Custom Favicon Controller

Spring Boot provides options to completely disable favicon by setting spring.mvc.favicon.enabled property to false, however, a browser may throw 404 error with the above approach. To handle this issue, create a custom favicon controller.

@GetMapping("favicon.ico")
@ResponseBody
public void disableFavicon() {
 //Method is void to avoid browser 404 issue by returning nothing in the response.
}

 

Summary

In this post, we discussed the process to override favicon in the Spring Boot application. We learned how to create a custom favicon in Spring Boot application.

Umesh

Hello!! I am Umesh- an engineer by profession and a photographer by passion.I like to build stuff on the web using OSS and love to capture the world through my lens.

follow me on:

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of