Created
November 8, 2021 10:15
-
-
Save michael-simons/afb6bbd2f4b67700cc5509084721cea2 to your computer and use it in GitHub Desktop.
Configure additional, dynamic paths to be delivered as static content by Spring Boot in addition to the default static content.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import java.util.List; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.context.properties.ConfigurationProperties; | |
import org.springframework.boot.context.properties.EnableConfigurationProperties; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; | |
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | |
@Configuration | |
public class ExtResourcesConfig { | |
public static final class PrefixAndLocation { | |
private String path; | |
private String location; | |
public String getPath() { | |
return path; | |
} | |
public void setPath(String path) { | |
this.path = path; | |
} | |
public String getLocation() { | |
return location; | |
} | |
public void setLocation(String location) { | |
this.location = location; | |
} | |
} | |
/** | |
* Use with | |
* <p> | |
* <code> | |
* ms.additional-resources[0].path = /x/** | |
* ms.additional-resources[0].location = file:///Users/msimons/tmp/r/ | |
* </code> | |
*/ | |
@ConfigurationProperties(prefix = "ms") | |
public static class ExResourcesProperties { | |
List<PrefixAndLocation> additionalResources = List.of(); | |
public List<PrefixAndLocation> getAdditionalResources() { | |
return additionalResources; | |
} | |
public void setAdditionalResources(List<PrefixAndLocation> additionalResources) { | |
this.additionalResources = additionalResources; | |
} | |
} | |
Logger logger = Logger.getLogger(ExtResourcesConfig.class.getName()); | |
@Bean | |
ExResourcesProperties exResourcesProperties() { | |
return new ExResourcesProperties(); | |
} | |
@Bean | |
WebMvcConfigurer registerAdditionalResources(@Autowired ExResourcesProperties properties) { | |
return new WebMvcConfigurer() { | |
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { | |
if (properties.additionalResources.isEmpty()) { | |
return; | |
} | |
properties.additionalResources.forEach(resource -> { | |
logger.log(Level.INFO, "Adding {0} with location {1}", new Object[] { resource.getPath(), resource.getLocation() }); | |
registry.addResourceHandler(resource.getPath()).addResourceLocations(resource.getLocation()); | |
}); | |
} | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment