From 8dbaa4a46688b172e63cf861e9bfc0106785de60 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 23 Aug 2013 16:39:40 -0700 Subject: [PATCH] Add support for web-jar resources Include resource mapping for web-jar resources. Issue: #55752928 --- .../web/WebMvcAutoConfiguration.java | 2 ++ .../web/WebMvcAutoConfigurationTests.java | 34 +++++++++++++++++++ 2 files changed, 36 insertions(+) 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 d7649be5be9..3346ebcabbf 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 @@ -165,6 +165,8 @@ public class WebMvcAutoConfiguration { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { + registry.addResourceHandler("/webjars/**").addResourceLocations( + "classpath:/META-INF/resources/webjars/"); registry.addResourceHandler("/**").addResourceLocations(RESOURCE_LOCATIONS); } 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 648f86ae550..124d1fb1b31 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 @@ -16,6 +16,9 @@ package org.springframework.boot.autoconfigure.web; +import java.lang.reflect.Field; +import java.util.LinkedHashMap; +import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; @@ -31,12 +34,19 @@ import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory import org.springframework.boot.context.embedded.MockEmbeddedServletContainerFactory; 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.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.handler.SimpleUrlHandlerMapping; +import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; import org.springframework.web.servlet.view.AbstractView; +import static org.hamcrest.Matchers.equalTo; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; /** * Tests for {@link WebMvcAutoConfiguration}. @@ -76,6 +86,30 @@ public class WebMvcAutoConfigurationTests { assertEquals(6, this.context.getBeanNamesForType(HandlerMapping.class).length); } + @Test + @SuppressWarnings("unchecked") + public void resourceHandlerMapping() throws Exception { + this.context = new AnnotationConfigEmbeddedWebApplicationContext(); + this.context.register(Config.class, WebMvcAutoConfiguration.class); + this.context.refresh(); + SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) this.context + .getBean("resourceHandlerMapping"); + Field locationsField = ReflectionUtils.findField( + ResourceHttpRequestHandler.class, "locations"); + locationsField.setAccessible(true); + Map> mappingLocations = new LinkedHashMap>(); + for (Map.Entry entry : mapping.getHandlerMap().entrySet()) { + ResourceHttpRequestHandler handler = (ResourceHttpRequestHandler) entry + .getValue(); + 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/"))); + } + @Configuration protected static class ViewConfig {