4

I started a fresh new project a couple of days ago using for the first time FreeMarker with Spring Boot 1.3. However, I'm struggling to display my own favicon. In fact, it worked well at the very beginning of the project but since a couple of days ago, it doesn't and I can't find out why. I already get through the three threads on stackoverflow talking about it but none fixed my issue. I searched on Google but I couldn't find any solution.

How to reproduce

Trying to get rid of the problem, I've started a new project (Spring 1.2.5 this time) and I got the same issue. Using Spring Tool Suite : New ► Spring Starter Project ► then I ticked Web and FreeMarker ► Finish.

Once the project ready, I created HomeController in demo.web package with one test function returning "home". I've also created a home.ftl into src/main/resources/templates and put two files in src/main/resources/static : demo.png and favicon.ico (I also tried to place it under src/main/resources).

The demo.png is showing correctly but the favicon.ico is not displayed. Perhaps I'm doing it horribly wrong as I'm a novice in web development.

HomeController.java

package demo.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class HomeController {

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String get() {
        return "home";
    }
}

home.ftl

<!DOCTYPE html>
<html>
<head>
    <title>Demo</title>
    <link rel="icon" type="image/x-icon" href="/favicon.ico">
</head>
<body>
    <img src="/demo.png" alt="">
</body>
</html>

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.test</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

If you need further information please ask me. I thank you all in advance for your help.

Best regards, Stilleur

Edit

Actually, it looks like Spring Boot overrides every favicon.ico he can found in the resource locations.

Cédric M.
  • 1,142
  • 3
  • 12
  • 23
  • have you set up a resources controller? check this answer : http://stackoverflow.com/questions/19955117/spring-controller-mapping-configuration-and-static-resources and check this answer : http://stackoverflow.com/questions/3234907/right-out-of-the-box-cache-control-header-filter for caching static resources at client's browser – AntJavaDev Jul 02 '15 at 20:03
  • There is no needs to set it up in Spring Boot as the org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration do it for "classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/" and "classpath:/public/". Proof is I can display my demo.png – Cédric M. Jul 02 '15 at 20:29
  • you can see it at your at browser because you loading it with src url not with href , thus the server knows it is an internal file.Also I am not so sure that you have configured the spring boot correctly as you havent posted your configuration , you can double check it at your browser if you open the network tab , where it shows all the requests on / off the browser , you could check there that you get a 404 for the favicon – AntJavaDev Jul 03 '15 at 00:58
  • 4
    add `spring.mvc.favicon.enabled=false` to your `application.properties`. – M. Deinum Jul 03 '15 at 07:52
  • 1
    @AntJavaDev I'm using the Spring Boot configuration so when I try to get my /favicon.ico it proposes me to download it (the Spring favicon, not mine). – Cédric M. Jul 03 '15 at 10:39
  • @M.Deinum I already tried it and I'll retry it but the last time it didn't change anything. – Cédric M. Jul 03 '15 at 10:40
  • @M.Deinum I've just installed the last **STS (3.7.0)** with the last **Spring Boot (1.3.0 M1)**. I did the same things as the "How to reproduce" section of my original question and add the `spring.mvc.favicon.enabled=false` to the `application.properties` and **it's working**. One last thing, can you repost your solution using **Post your answer** so I can accept it as the solution. Thank a lot M.Deinum ! – Cédric M. Jul 03 '15 at 11:20

2 Answers2

8

Please rename your favicon.ico to some different ico file, e.g. mycustomfavicon.ico. Replace correct name in your href as well and it will work.

YoungHobbit
  • 13,254
  • 9
  • 50
  • 73
mirec
  • 627
  • 1
  • 8
  • 23
3

Since @M.Deinum hasn't reposted his comment as an answer, I'll do it.

For Spring Boot 1.3.0.RELEASE, simply add the following line into your application.properties: spring.mvc.favicon.enabled=false

According to @Stilleur, this has worked since at least Spring Boot 1.3.0.M1.

Kenco
  • 683
  • 9
  • 21