diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java index 65ef9be6c5..8d0e8bb7fa 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java @@ -17,6 +17,7 @@ package org.springframework.web.servlet.config.annotation; import java.util.ArrayList; +import java.util.Arrays; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -64,6 +65,7 @@ public class ResourceHandlerRegistry { this.servletContext = servletContext; } + /** * Add a resource handler for serving static resources based on the specified URL path patterns. * The handler will be invoked for every incoming request that matches to one of the specified path patterns. @@ -75,6 +77,18 @@ public class ResourceHandlerRegistry { return registration; } + /** + * Whether a resource handler has already been registered for the given pathPattern. + */ + public boolean hasMappingForPattern(String pathPattern) { + for (ResourceHandlerRegistration registration : registrations) { + if (Arrays.asList(registration.getPathPatterns()).contains(pathPattern)) { + return true; + } + } + return false; + } + /** * Specify the order to use for resource handling relative to other {@link HandlerMapping}s configured in * the Spring MVC application context. The default value used is {@code Integer.MAX_VALUE-1}. diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistryTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistryTests.java index 816cdc8222..fdaed8a8b4 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistryTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistryTests.java @@ -16,9 +16,6 @@ package org.springframework.web.servlet.config.annotation; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - import org.junit.Before; import org.junit.Test; import org.springframework.mock.web.test.MockHttpServletRequest; @@ -29,6 +26,8 @@ import org.springframework.web.servlet.HandlerMapping; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import org.springframework.web.servlet.resource.ResourceHttpRequestHandler; +import static org.junit.Assert.*; + /** * Test fixture with a {@link ResourceHandlerRegistry}. * @@ -84,6 +83,13 @@ public class ResourceHandlerRegistryTests { assertEquals(0, registry.getHandlerMapping().getOrder()); } + @Test + public void hasMappingForPattern() { + assertTrue(registry.hasMappingForPattern("/resources/**")); + assertFalse(registry.hasMappingForPattern("/whatever")); + } + + private ResourceHttpRequestHandler getHandler(String pathPattern) { SimpleUrlHandlerMapping handlerMapping = (SimpleUrlHandlerMapping) registry.getHandlerMapping(); return (ResourceHttpRequestHandler) handlerMapping.getUrlMap().get(pathPattern);