diff --git a/spring-boot-modules/spring-boot-basic-customization/README.md b/spring-boot-modules/spring-boot-basic-customization/README.md index ce4e29001c..a3d9f1b1fc 100644 --- a/spring-boot-modules/spring-boot-basic-customization/README.md +++ b/spring-boot-modules/spring-boot-basic-customization/README.md @@ -10,3 +10,4 @@ This module contains articles about Spring Boot customization - [Spring Boot: Customize Whitelabel Error Page](https://www.baeldung.com/spring-boot-custom-error-page) - [Spring Boot: Configuring a Main Class](https://www.baeldung.com/spring-boot-main-class) - [How to Define a Spring Boot Filter?](https://www.baeldung.com/spring-boot-add-filter) + - [Guide to the Favicon in Spring Boot](https://www.baeldung.com/spring-boot-favicon) diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/favicon/FaviconApplication.java b/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/favicon/FaviconApplication.java new file mode 100644 index 0000000000..f94549b988 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/favicon/FaviconApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.favicon; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class FaviconApplication { + + public static void main(String[] args) { + SpringApplication.run(FaviconApplication.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/favicon/config/FaviconConfiguration.java b/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/favicon/config/FaviconConfiguration.java new file mode 100644 index 0000000000..d1809199d6 --- /dev/null +++ b/spring-boot-modules/spring-boot-basic-customization/src/main/java/com/baeldung/favicon/config/FaviconConfiguration.java @@ -0,0 +1,45 @@ +package com.baeldung.favicon.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.Resource; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; +import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +@Configuration +public class FaviconConfiguration { + + @Bean + public SimpleUrlHandlerMapping myFaviconHandlerMapping() { + 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("com/baeldung/images"); + List locations = Arrays.asList(classPathResource); + requestHandler.setLocations(locations); + return requestHandler; + } + + // @Controller + static class FaviconController { + + @RequestMapping(value = "favicon.ico", method = RequestMethod.GET) + @ResponseBody + void favicon() { + } + } +} diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/resources/application.properties b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/application.properties index 2063d36b46..4d4a6ec7bd 100644 --- a/spring-boot-modules/spring-boot-basic-customization/src/main/resources/application.properties +++ b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/application.properties @@ -4,4 +4,4 @@ #spring.banner.image.width= //TODO #spring.banner.image.height= //TODO #spring.banner.image.margin= //TODO -#spring.banner.image.invert= //TODO \ No newline at end of file +#spring.banner.image.invert= //TODO diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/resources/com/baeldung/images/favicon.ico b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/com/baeldung/images/favicon.ico new file mode 100644 index 0000000000..64105ac11c Binary files /dev/null and b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/com/baeldung/images/favicon.ico differ diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/resources/favicon.ico b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/favicon.ico new file mode 100644 index 0000000000..64105ac11c Binary files /dev/null and b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/favicon.ico differ diff --git a/spring-boot-modules/spring-boot-basic-customization/src/main/resources/static/favicon.ico b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/static/favicon.ico new file mode 100644 index 0000000000..64105ac11c Binary files /dev/null and b/spring-boot-modules/spring-boot-basic-customization/src/main/resources/static/favicon.ico differ