Spring Boot custom favicon.ico not showing

Viewed 22771

I know this question has been asked over and over here and there are several solutions. I've tried several of those except the ones that suggests writing you own configuration bean for this. I don't want to do all that just to display a tiny icon it seams overkill. But I can not get it to work. These are the solutions I've tried so far.

  1. just add favicon.ico under static resources and it should work....it doesn't.
  2. spring.mvc.favicon.enabled=false in application.properties, no favicon showed at all (which I guess is the whole point of that).
  3. Tried 2 examples of including the favicon as a link in the html pages. Like so: <link rel="icon" type="image/png" href="favicon.png" /> <link rel="icon" type="image/x-icon" href="favicon.ico" />

Neither of those work.

  1. Tried renaming my own favicon to something else and reference it as above. Does not work.

When inspecting the page in the browser I sometimes get no error at all printed out despite no icon showing, or I get an error saying GET http://localhost:8080/myapp/favicon.png 404 () Where it is refering the type as JSON (which I find strange).

I'm running out of ideas here so if anyone can tell me why this is not working please let me know. Did I perhaps forget one of those magic spring annotations? This is what my main class looks like.

@SpringBootApplication 
@ComponentScan
@Configuration
@EnableWebMvc
public class JobengineMonitorApplication implements CommandLineRunner {

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

 }

I am using thymeleaf as the template engine

10 Answers

Put your favicon.png under src/main/resources/public and add this to your *.html page exactly in the header section

 <link rel="shortcut icon" type="image/png" th:href="@{favicon.png}"/>

If anyone faces the same problem using newer version of Spring (in my case spring boot 2.4.4), here's the scenario that worked fine for me:

  1. Put the favicon.ico in the /resources/static folder. I also tried to put it just in the /resourses/ folder and it worked fine as well, so do not worry about the folder that much.
  2. Create a FaviconConfiguration in your configurations folder with the following content:
@Configuration
public class FaviconConfiguration {

    @Bean
    public SimpleUrlHandlerMapping customFaviconHandlerMapping() {
        SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
        mapping.setOrder(Integer.MIN_VALUE);
        mapping.setUrlMap(Collections.singletonMap(
                "/static/favicon.ico", faviconRequestHandler()));
        return mapping;
    }

    @Bean
    protected ResourceHttpRequestHandler faviconRequestHandler() {
        ResourceHttpRequestHandler requestHandler
                = new ResourceHttpRequestHandler();
        requestHandler.setLocations(Collections.singletonList(new ClassPathResource("/")));
        return requestHandler;
    }
}
  1. In case you use Spring Security don't forget to add antMatcher for your favicon resource by adding the following code to your SpringSecurityConfiguration (as JaneXQ already mentioned above):
.antMatchers("/static/favicon.ico").permitAll()
  1. Use link to your custom favicon explicitly in your html. To do that just put the following link in the <head> section of each of your html pages in the following way (I used thymeleaf in here):
<head>
    ...
    <link rel="icon" type="image/ico" th:href="@{../static/favicon.ico}">
    ...
</head>

I found I had to put my favicon.ico file in:

src/main/resources/public

I saved my favicon which was a simple .png download as src/main/resources/static/favicon.ico

I couldn't get it to display until I tried another browser and it worked fine - so try clearing the browser cache, or try testing on another browser

Ok so this appears to be working now. Of course I managed to get it working just after ranting about it :).

Anyway, what i did was.

  1. Remove @EnableWebMvc from the main class
  2. For added ../ to the href depending on the urls e.g /index was fine but /edit/something.html was not

Sorry for wasting peoples time but hopefully this could be useful for another rookie like me

Try to replace th:href with href. It worked for me.

<link rel="icon" href="/favicon.ico" type="image/ico">

For some reason .ico format was not working. I just placed a png image instead and spring automatically picked the favicon.

I placed the png image in \src\main\resources\public

Spring boot + thymeleaf

I had the same issue and fix it removing @EnableAdminServer annotation

Related