JAVA-86: spring-boot-favicon

This commit is contained in:
Krzysiek 2020-03-15 21:46:59 +01:00
parent b19dbb9c20
commit e8eba5fe3d
7 changed files with 59 additions and 1 deletions

View File

@ -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)

View File

@ -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);
}
}

View File

@ -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<Resource> locations = Arrays.asList(classPathResource);
requestHandler.setLocations(locations);
return requestHandler;
}
// @Controller
static class FaviconController {
@RequestMapping(value = "favicon.ico", method = RequestMethod.GET)
@ResponseBody
void favicon() {
}
}
}

View File

@ -4,4 +4,4 @@
#spring.banner.image.width= //TODO
#spring.banner.image.height= //TODO
#spring.banner.image.margin= //TODO
#spring.banner.image.invert= //TODO
#spring.banner.image.invert= //TODO

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB