5

(I examined similar questions, but none of them explain the odd behavior I illustrate at the end of this question.)

I have a Spring Boot 1.3.5 application that insists on replacing my favicon with Boot's default favicon (the green leaf). To resolve the problem, I have tried the following:

  1. Install my own favicon at my application's static root.

The word on the street is that this is just supposed to work. Unfortunately, it does not.

  1. Set property spring​.​mvc​.​favicon​.​enabled to false.

This is supposed to disable org​.​springframework​.​boot​.​autoconfigure​.​web​.​WebMvcAutoConfiguration​.​WebMvcAutoConfigurationAdapter​.​FaviconConfiguration, which appears to responsible for serving Boot's default favicon. By setting a breakpoint in that class, I was able to confirm that the beans defined in that class indeed do not get created when the property is set to false.

Unfortunately, this didn't solve the problem, either.

  1. Implement my own favicon handler:

    @Configuration
    public class FaviconConfiguration {
    
        @Bean
        public SimpleUrlHandlerMapping faviconHandlerMapping() {
            SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
            mapping.setOrder(Integer.MIN_VALUE);
            mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", faviconRequestHandler()));
            return mapping;
        }
    
        @Bean
        protected ResourceHttpRequestHandler faviconRequestHandler() {
            ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
            ClassPathResource classPathResource = new ClassPathResource("static/");
            List<Resource> locations = Arrays.asList(classPathResource);
            requestHandler.setLocations(locations);
            return requestHandler;
        }
    
    }
    

Sadly, no luck here, too.

  1. Rename my favicon from favicon.ico to logo.ico, and just point all my pages' favicon links to that, instead.

Now, with this potential fix, I discovered a surprising result. When I curled my newly named icon.ico resource, I was served Spring's leaf icon. However, when I deleted the resource, I got 404. But then, when I put it back, I got the leaf again! In other words, Spring Boot was happy to answer 404 when my static resource was missing, but when it was there, it would always answer with the leaf instead!

Incidentally, other static resources (PNGs, JPGs, etc.) in the same folder serve up just fine.

It was easy to imagine that there was some evil Spring Boot contributor laughing himself silly over this, as I pulled my hair out. :-)

I'm out of ideas. Anyone?

As a last resort, I may just abandon using an ICO file for my site icon, and use a PNG instead, but that comes at a cost (losing multi-resolution support), so I'd rather avoid that.

Sharky
  • 549
  • 5
  • 14

2 Answers2

1

This is a spring boot feature:

Spring MVC auto-configuration

Spring Boot provides auto-configuration for Spring MVC that works well with most applications.

The auto-configuration adds the following features on top of Spring’s defaults:

  1. Inclusion of ContentNegotiatingViewResolver and BeanNameViewResolver beans.
  2. Support for serving static resources, including support for WebJars (see below).
  3. Automatic registration of Converter, GenericConverter, Formatter beans.
  4. Support for HttpMessageConverters (see below).
  5. Automatic registration of MessageCodesResolver (see below).
  6. Static index.html support.
  7. Custom Favicon support.
  8. Automatic use of a ConfigurableWebBindingInitializer bean (see below).

You can find this document at: http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-auto-configuration

And, If you want to disable spring boot favicon, you can add this config to you yml or perperties files

spring.mvc.favicon.enabled=true # Enable resolution of favicon.ico.

Or, If you want change favicon to you own. try this:

@Configuration
public static class FaviconConfiguration {

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

And you can find more detail at: Spring Boot: Overriding favicon

UPDATE:

put favicon.ico to resources folder.

put favicon.ico to resources

And, try it:

favicon

Community
  • 1
  • 1
BeeNoisy
  • 1,254
  • 1
  • 14
  • 23
  • 2
    The question is looking for additional ideas that one might pursue, that are not already mentioned in the question. Unfortunately, this answer does not acknowledge that the solutions it attempts to provide are already given in the question. Further, one of the answers it provides is wrong. You'd want to set ....favicon.enabled to false, not true. Lastly, this answer provides unnecessary details not related to favicon configuration. – Sharky Nov 09 '16 at 18:03
  • I am using spring boot 1.4.1-RELEASE, this is work for me.I just update my answer. By the way, I have not change any config in yml. – BeeNoisy Nov 10 '16 at 04:18
  • The updated part of the answer solved the problem for me. Strange, that changing the favicon is so poorly documented. – Florian Fankhauser Jan 07 '17 at 21:13
  • @BeeNoisy I have seen many threads that if we just add "favicon.ico" into resources folder it will automatically pick up. For some odd reason my application is not behaving like this. – JayC Jan 28 '20 at 16:00
0

Why choose the hard way, when u can get the easy one?

just create a new link into ur <head> with:

<link rel="icon" type="image/png" href="images/fav.png" />

Copy and paste ur icon in src/main/resources/static/images/

Rename the file to whatever you want, just remember to change the link in the html too.

Dwhitz
  • 1,250
  • 7
  • 26
  • 38
Nirvana
  • 31
  • 6