Spring Boot - Using Custom Favicon

[Updated: Sep 7, 2017, Created: Aug 20, 2017]

In Spring Boot application, we can specify a custom favicon by placing favicon.ico (exactly with this name) in any of the static content locations.

Example

Our custom favicon.ico

src/main/resources/favicon.ico

A MVC controller

@Controller
public class MyController {

  @RequestMapping("/app")
  public String handler(Model model){
      model.addAttribute("msg", "a message from controller");
      return "my-page";
  }
}

The JSP page

src/main/webapp/WEB-INF/pages/my-page.jsp

<%@ page language="java"
    contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<body>
    <h2>A JSP page </h2>
    Message : ${msg}
</body>
</html>

Boot properties

src/main/resources/application.properties

spring.mvc.view.prefix= /WEB-INF/pages/
spring.mvc.view.suffix= .jsp

Main class

@SpringBootApplication
public class ExampleMain{

  public static void main(String[] arg) {
      SpringApplication.run(ExampleMain.class, arg);
  }
}

To try examples, run spring-boot maven plugin (configured in pom.xml of example project below):

mvn spring-boot:run

Output

As seen in above screenshot, our custom icon is showing up instead of the default leaf icon.

Example Project

Dependencies and Technologies Used :

  • spring-boot-starter-web 1.5.6.RELEASE: Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container.
    Corresponding Spring version: 4.3.10.RELEASE
  • spring-boot-starter-tomcat 1.5.6.RELEASE: Starter for using Tomcat as the embedded servlet container. Default servlet container starter used by spring-boot-starter-web.
    Corresponding Spring version: 4.3.10.RELEASE
  • tomcat-embed-jasper 8.5.16: Core Tomcat implementation.
  • JDK 1.8
  • Maven 3.3.9

Custom Favicon Example Select All Download
  • custom-favicon-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
        • webapp
          • WEB-INF
            • pages

See Also