(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:
- 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.
- 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.
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.
- 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 curl
ed 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.