Search code examples
springspring-mvcfaviconrequest-mapping

Spring @RequestMapping("/favicon.ico") does not work when requested from index.html


I used to get mapping not found for favicon.ico so decided to deal with it.

Easiest thing for me was to add an action to a controller method,@RequestMapping("/favicon.ico").

I no longer get these complaints ( although I don't request favicon.ico myself in an html file, I guess teh browser does this automatically ).

When I visit http://localhost:8080/favicon.ico the action gets hit!

I add the following to my html file:

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

But the action never gets hit.

I've also tried

<link href="http://localhost:8080/favicon.ico" rel="icon" type="image/x-icon" />

but action does not get hit.

I suspect this has to do with get/post request something, and when I request it from the browser manually, a get request is made. When from an html file something else, and Spring won't recognize this.

Please do not recommend me doing:

<mvc:resources mapping="/favicon.ico" location="/favicon.ico" />

as I like to do it from my controller as I have some logic there.

Could there be some cache involved?

EDIT:

I should also mention that i keep getting a tomcat favicon instead. Can't see any info on a favicon.ico ever being requested. Is tomcat supplying it by default and ignoring to hit/forward it to my action?


Solution

  • I ran into the same issue.

    When spring boot starts you can see this in the console:

    [...] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    

    You can disable the spring mvc favicon handler from the application.properties:

    spring.mvc.favicon.enabled=false
    

    Source: Spring Boot: Overriding favicon