Sharky
Sharky

Reputation: 559

Why does Spring Boot replace all of my favicons with Spring's leaf icon?

(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.

Upvotes: 5

Views: 9247

Answers (2)

Nirvana
Nirvana

Reputation: 31

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.

Upvotes: 0

BeeNoisy
BeeNoisy

Reputation: 1384

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

Upvotes: 1

Related Questions