0

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?

Aniket Kulkarni
  • 12,825
  • 9
  • 67
  • 90
mjs
  • 21,431
  • 31
  • 118
  • 200
  • Open your browser's network console and check exactly what request is sent when you click the link. – Sotirios Delimanolis Sep 21 '15 at 15:49
  • Also, enable Spring's DEBUG logging and see what Spring is doing. – Sotirios Delimanolis Sep 21 '15 at 15:49
  • @SotiriosDelimanolis I can't see anything in the browser network suggesting that the favicon.ico is ever requested which would explain why my action is not being hit. But I don't think that is normally included in network info. Favicon is special. I should also mention that I keep getting the tomcat favicon.ico logo instead. Without specifiying it as well. – mjs Sep 21 '15 at 15:53
  • Are you sure it's `http://localhost:8080/favicon.ico` you're sending the request to? Is your app's context path really empty? – Sotirios Delimanolis Sep 21 '15 at 15:57
  • Note, if I change the favicon.ico to favicon.png it appears in the network log. However, favicon.ico seems to be supplied in the original request, and is being cached for some reason. – mjs Sep 21 '15 at 15:57
  • I just ran into this: http://stackoverflow.com/a/22659172/961018 – mjs Sep 21 '15 at 16:00
  • This is not Spring. Spring would log if it did anything. The request isn't sent by the browser. – Sotirios Delimanolis Sep 21 '15 at 16:50
  • What browser are you on? I think it simply ignores that request. It's probably documented for your specific browser somewhere. – Sotirios Delimanolis Sep 21 '15 at 16:52
  • @SotiriosDelimanolis I've tried a couple of browsers, and just tried another one, and it did switch. I have cache disabled on chrome and firefox but that didn't do the trick. I then on chrome CTRL + SHIFT + DELETE and cleared the cache. That didn't do the trick either, but then I tried to do it again and restarted the browser and voila it worked. It seems mighty hard to get that favicon refreshed! But it works now. Seems to have been a browser caching issue, where it refused to refresh. – mjs Sep 21 '15 at 17:00
  • Thanks for your help :) – mjs Sep 21 '15 at 17:00
  • It is not solution, if you think you have got rid of favicon 404 then keep your developer console open and you will not be disappointed to know that again you are getting favicon 404. – hagrawal7777 Sep 21 '15 at 17:03
  • @hagrawal It's not a 404. The browser was not making the request at all. – Sotirios Delimanolis Sep 21 '15 at 17:09
  • @SotiriosDelimanolis If browser is not making this request then who is making this request? Let's talk about default case when there is no code to fetch favicon. Then why do you get 404? As per best of my knowledge - if browser is Favicon supported then it will try to load favicon and display the icon next to site's name. – hagrawal7777 Sep 21 '15 at 17:17
  • @ha No one is making the request. There is no request sent. – Sotirios Delimanolis Sep 21 '15 at 17:18
  • @SotiriosDelimanolis It not from code but browser makes this request if it is favicon supported, I have already mentioned this. You can check - it will end up with 404 and is a GET request. If browser and code is not making this request then who is making? – hagrawal7777 Sep 21 '15 at 17:28
  • @momo Can you clarify whether you are getting 404 and this is a GET request from your browser or not? – hagrawal7777 Sep 21 '15 at 17:28
  • @hagrawal That's the problem. Nothing is sending the request. OP's first comment: _I can't see anything in the browser network suggesting that the favicon.ico is ever requested_ – Sotirios Delimanolis Sep 21 '15 at 17:41
  • @SotiriosDelimanolis Ok. But we cannot overlook his first like "*I used to get mapping not found for favicon.ico so decided to deal with it. *". As per me browser will make this request if it is favicon supported .. I have added a image in my answer edit, taking an example of my application to recreate it (please note I was having no code in JS/HTML to load favicon) – hagrawal7777 Sep 21 '15 at 17:56
  • @hagrawal You're completely misunderstanding this question. Step 1. I didn't work. Step 2. They made it work by adding a handler. Step 3. They added a `link` tag to their HTML header. Step 4. favicon was no longer requested. Step 5. They asked this question about why it was no longer requested. – Sotirios Delimanolis Sep 21 '15 at 17:58
  • I am sure that either of us is misunderstanding the question, but cannot say who!!! OP knew he was not requesting favicon and guessed it browser might be - "*although I don't request favicon.ico myself in an html file, I guess teh browser does this automatically*" .. He added a mapping and he was no longer getting errors but when he tried from HTML. And answer is simple, like I said - "favicon is a app resource and not a data which can be fetched from server through mapping" .. – hagrawal7777 Sep 21 '15 at 18:10
  • You cannot use ` – hagrawal7777 Sep 21 '15 at 18:11
  • @hagrawal i've mentioned in the question that i do just that. The conclusion was that the browser was caching it, and refusing to refresh. That's the correct answer. – mjs Sep 21 '15 at 18:34
  • @momo "*Can you clarify whether you are getting 404 and this is a GET request from your browser or not?*" – hagrawal7777 Sep 21 '15 at 18:41
  • Your question is *Spring @RequestMapping(“/favicon.ico”) does not work when requested from index.html* .. As per me correct answer is that favicon is not a data which can fetched from server through mapping, it is a resource .. When you use it from HTML then it is fetched as a resource and your mapping will never be invoked while when you do from browser address bar then it is intercepted by Spring and will be invoked, and all this has nothing to do with cache .. Try to hit from browser address bar and mapping will always be invoked but not from HTML . – hagrawal7777 Sep 21 '15 at 18:46
  • I have said this over and over again, and one more last time **/favicon.ico is not a server call for data but it is a server call for a resource, which in this case is an icon or a image** .. As per best of my knowledge is 100% true .. – hagrawal7777 Sep 21 '15 at 18:46
  • @hagrawal You have clients and you have servers. You have sockets (and wires) in between them. The client makes a request. The server sends a response. In HTTP, a client sends an HTTP request to a URL through the socket. The server receives the HTTP request and replies with an HTTP response. What you call _data_ and _resource_ are super high level almost to be meaningless. The server just sends byte over the wire. In this case, the browser (the client), never makes an HTTP request, because of some caching issue. – Sotirios Delimanolis Sep 21 '15 at 19:33
  • @hagrawal Think of the server as a black box. You can't see how it's implemented. All you know is that it sends HTTP responses. – Sotirios Delimanolis Sep 21 '15 at 19:41
  • @SotiriosDelimanolis I will agree to what you said above, computers only understand 0's and 1's, so with that everything is meaningless. Simple question I have been asking-can you solve his favicon issue by Spring mapping(in the end icon should be displayed next to site's name)? If I am correct then you guys (both) are pretty experienced than me and I do have respect for that. Having said that, OP's approach itself is wrong, he was trying to resolve the favicon issue by using a mapping, and any answer which doesn't correct that, is a wrong answer. And I have been trying to correct his approach – hagrawal7777 Sep 21 '15 at 20:41
  • @hagrawal Yes, you can create a handler method to handle a request to `/favicon.ico` just like any other path. **His issue is not with the handler method.** – Sotirios Delimanolis Sep 21 '15 at 20:54
  • @SotiriosDelimanolis Then how you will display favicon next to site's name, and all the purpose which favicon solves? – hagrawal7777 Sep 21 '15 at 20:58
  • @hagrawal The action of displaying an image is the responsibility of the client. The client (the browser) makes an HTTP request to the server, the server responds with an HTTP response containing binary (that is to be interpreted as `image/x-icon` content). The client receives that content and then renders it in some way. This is the same process as rendering the `Add Comment` button next right below this comment. The server is not involved at all. – Sotirios Delimanolis Sep 21 '15 at 22:48
  • @SotiriosDelimanolis But if somebody is using a favicon or any app resource then it is his responsibility to render it on page and not just fetching from server. Until end-to-end solution'ing is not done, issue is not resolved. I will reiterate - **OP's approach of solving favicon issue itself is wrong, he was trying to resolve the favicon issue by using a Spring mapping, and any answer which doesn't correct that, is a wrong answer. And I have been trying to correct his approach** – hagrawal7777 Sep 22 '15 at 10:37
  • @hagrawal There aren't a million ways to say this. Even if they did what you are suggesting and served the image content through some other mechanism, **they'd still have the same exact problem**. The issue is not how they serve it, the issue is the browser caching. – Sotirios Delimanolis Sep 22 '15 at 15:18
  • @hagrawal You have a serious misunderstanding of web stacks if you think _it is his responsibility to render it on page_. That's not how the web works (browsers in this case). – Sotirios Delimanolis Sep 22 '15 at 15:18
  • @SotiriosDelimanolis They surely have made some mistake in implementing the approach correctly. There aren't million ways to implement the Favicon, its pretty simple and straight forward, I have implemented in our web app and million others using same successful approach .. – hagrawal7777 Sep 22 '15 at 15:35
  • @SotiriosDelimanolis I can accept this and would rephrase it, what I meant was its developers responsibility to make sure that he has achieved what he was trying .. If he is trying to use the Favicon then just fetching from server or defining a server mapping is not enough, until the favicon is displayed next to site's name etc. is not done, his job is not finished .. – hagrawal7777 Sep 22 '15 at 15:36
  • @hagrawal I'm going to stop after this because you don't seem to understand what I'm saying. The concept of favicon is a convention. From the wikipedia article that you linked to: _Browsers that provide favicon support typically display a page's favicon in the browser's address bar [...]_. It is the responsibility of the browser to display it. All it needs from the server (your web application) are the bytes which it gets in an HTTP response to its HTTP request. How the server gets those bytes does not matter to the browser. – Sotirios Delimanolis Sep 22 '15 at 15:58
  • @ha The OP has already stated that their handler method works and serves the bytes correctly. Their issue was with the browser (something mostly out of their control), not with the server. – Sotirios Delimanolis Sep 22 '15 at 15:59

1 Answers1

0

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

gaetanc
  • 116
  • 5