From 64d9f4f18d09b2632e5d8f9c9ad9e5abe3777953 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Wed, 9 Oct 2013 13:13:39 -0400 Subject: [PATCH] Allow user to override static resource handler mappings A new API in Spring allows us to check for existing mappings in the resource handler registry --- .../web/WebMvcAutoConfiguration.java | 11 +++- .../web/WebMvcAutoConfigurationTests.java | 62 +++++++++++++++++-- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java index a96003787c..d76f52896e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java @@ -175,9 +175,14 @@ public class WebMvcAutoConfiguration { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { - registry.addResourceHandler("/webjars/**").addResourceLocations( - "classpath:/META-INF/resources/webjars/"); - registry.addResourceHandler("/**").addResourceLocations(RESOURCE_LOCATIONS); + if (!registry.hasMappingForPattern("/webjars/**")) { + registry.addResourceHandler("/webjars/**").addResourceLocations( + "classpath:/META-INF/resources/webjars/"); + } + if (!registry.hasMappingForPattern("/**")) { + registry.addResourceHandler("/**").addResourceLocations( + RESOURCE_LOCATIONS); + } } @Override diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java index 124d1fb1b3..fa0fb09782 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfigurationTests.java @@ -40,6 +40,8 @@ import org.springframework.util.ReflectionUtils; import org.springframework.web.servlet.HandlerAdapter; import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.View; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; import org.springframework.web.servlet.view.AbstractView; @@ -87,11 +89,43 @@ public class WebMvcAutoConfigurationTests { } @Test - @SuppressWarnings("unchecked") public void resourceHandlerMapping() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context.register(Config.class, WebMvcAutoConfiguration.class); this.context.refresh(); + Map> mappingLocations = getMappingLocations(); + assertThat(mappingLocations.get("/**").size(), equalTo(5)); + assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1)); + assertThat(mappingLocations.get("/webjars/**").get(0), + equalTo((Resource) new ClassPathResource("/META-INF/resources/webjars/"))); + } + + @Test + public void resourceHandlerMappingOverrideWebjars() throws Exception { + this.context = new AnnotationConfigEmbeddedWebApplicationContext(); + this.context.register(WebJars.class, Config.class, WebMvcAutoConfiguration.class); + this.context.refresh(); + Map> mappingLocations = getMappingLocations(); + assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1)); + assertThat(mappingLocations.get("/webjars/**").get(0), + equalTo((Resource) new ClassPathResource("/foo/"))); + } + + @Test + public void resourceHandlerMappingOverrideAll() throws Exception { + this.context = new AnnotationConfigEmbeddedWebApplicationContext(); + this.context.register(AllResources.class, Config.class, + WebMvcAutoConfiguration.class); + this.context.refresh(); + Map> mappingLocations = getMappingLocations(); + assertThat(mappingLocations.get("/**").size(), equalTo(1)); + assertThat(mappingLocations.get("/**").get(0), + equalTo((Resource) new ClassPathResource("/foo/"))); + } + + @SuppressWarnings("unchecked") + protected Map> getMappingLocations() + throws IllegalAccessException { SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) this.context .getBean("resourceHandlerMapping"); Field locationsField = ReflectionUtils.findField( @@ -104,10 +138,7 @@ public class WebMvcAutoConfigurationTests { mappingLocations.put(entry.getKey(), (List) locationsField.get(handler)); } - assertThat(mappingLocations.get("/**").size(), equalTo(5)); - assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1)); - assertThat(mappingLocations.get("/webjars/**").get(0), - equalTo((Resource) new ClassPathResource("/META-INF/resources/webjars/"))); + return mappingLocations; } @Configuration @@ -128,6 +159,27 @@ public class WebMvcAutoConfigurationTests { } + @Configuration + protected static class WebJars extends WebMvcConfigurerAdapter { + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/webjars/**").addResourceLocations( + "classpath:/foo/"); + } + + } + + @Configuration + protected static class AllResources extends WebMvcConfigurerAdapter { + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/**").addResourceLocations("classpath:/foo/"); + } + + } + @Configuration protected static class Config {