Spring Boot: Overriding favicon
You can just put your own favicon.ico in the root of the classpath or in any of the static resource locations (e.g. classpath:/static
). You can also disable favicon resolution completely with a single flag spring.mvc.favicon.enabled=false
.
Or to take complete control you can add a HandlerMapping (just copy the one from Boot and give it a higher priority), e.g.
@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;
}
@Bean
protected ResourceHttpRequestHandler faviconRequestHandler() {
ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
requestHandler.setLocations(Arrays
.<Resource> asList(new ClassPathResource("/")));
return requestHandler;
}
}
None of this was necessary for me.
Why override the default when you can bundle a resource with the generated JAR that will take higher precedence than the default one.
To achieve a custom favicon.ico
file, I created a src/main/resources
directory for my application and then copied the favicon.ico
file into there. The files in this resources directory are moved to the root of the compiled JAR and therefore your custom favicon.ico
is found before the Spring provided one.
Doing the above achieved the same effect as your updated solution above.
Note that since v1.2.0 you can also put the file in src/main/resources/static
.