Failed to install my custom favicon.ico in Spring Boot 1.3 or 1.2.5

A few days ago I started a new new project using FreeMarker for the first time with Spring Boot 1.3 . However, I am struggling to render my own icon. In fact, it worked well at the very beginning of the project, but since a couple of days ago it hasn't and I can't figure out why. I already go through three threads on stackoverflow talking about this, but no one has fixed my problem. I have searched on google but I could not find a solution.

How to reproduce

Trying to get rid of the problem, I started a new project (Spring 1.2.5 this time) and I got the same problem. With Spring Toolbox: New ► Spring Starter Project ► then I will mark the website and FreeMarker ► Finish.

Once the project is ready, I created a HomeController in the demo.web package with one test function returning "home". I also created a home.ftl in 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).

Demo.png is displayed correctly, but the favicon.ico is not displayed . Perhaps I am doing it terribly wrong as I am new to 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 more information, please ask me. I thank you all for your help.

Regards, Stilleur

Edit

It actually looks like Spring Boot overrides every favicon.ico it can find in resource locations.

+3


source to share


2 answers


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

For Spring Boot 1.3.0.RELEASE, just add the following line to application.properties

: spring.mvc.favicon.enabled=false



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

+3


source


Rename favicon.ico

to another file ico

eg. mycustomfavicon.ico

... Replace the correct name in href

and it will work.



+8


source







All Articles