Home > OS >  How to avoid Spring Boot Controller catching request for favicon?
How to avoid Spring Boot Controller catching request for favicon?

Time:10-01

I have the following Controller

@Controller
public class MyController {

    @GetMapping({"/", "/{name}"})
    public String hello(@PathVariable(required = false) String name) {
        System.out.println("Name: "   name);
        return "hello";
    }
}

and this directory structure

- resources
    - static
        - favicon.ico
    - templates
        - hello.html

When I GET http://localhost:8080/ through the browser, my application prints:

Name: null
Name: favicon.ico

So the request for /favicon.ico goes was caught by my controller, but instead I want that Spring handles this request and returns the favicon.ico placed here resources/static/favicon.ico

I think there should be a way without adding a handful of lines of configuration.

CodePudding user response:

One approach you could take is to disable favicon resolution via this property:

spring.mvc.favicon.enabled=false

CodePudding user response:

I had to include <link rel="icon" type="image/x-icon" href="images/favicon.ico"> in my html head, and it worked. The images prefix isnt necessary, but I moved the icon into their because I was allowing any unauthenticated request to /images so I dont need a extra rule for the favicon.ico. Thats my current directory strucutre:

- resources
    - static
        - images
            - favicon.ico
    - templates
        - hello.html

s/o to Ajay Kumar for helping.

  • Related