Introduce common SimpleUrlHandlerMapping constructors
Prior to this commit, the SimpleUrlHandlerMapping classes in Spring MVC and Spring Webflux only had default constructors. This lead to the fact that users often had to explicitly invoke setUrlMap() and setOrder() on the newly instantiated SimpleUrlHandlerMapping. In order to simplify the programmatic setup of a SimpleUrlHandlerMapping in common scenarios, this commit introduces the following constructors. - SimpleUrlHandlerMapping() - SimpleUrlHandlerMapping(Map<String, ?> urlMap) - SimpleUrlHandlerMapping(Map<String, ?> urlMap, int order) Closes gh-23362
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.web.servlet.config.annotation;
|
||||
import java.util.Collections;
|
||||
import javax.servlet.ServletContext;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.servlet.DispatcherServlet;
|
||||
@@ -85,9 +86,10 @@ public class DefaultServletHandlerConfigurer {
|
||||
|
||||
|
||||
/**
|
||||
* Return a handler mapping instance ordered at {@link Integer#MAX_VALUE} containing the
|
||||
* {@link DefaultServletHttpRequestHandler} instance mapped to {@code "/**"};
|
||||
* or {@code null} if default servlet handling was not been enabled.
|
||||
* Return a handler mapping instance ordered at {@link Ordered#LOWEST_PRECEDENCE}
|
||||
* containing the {@link DefaultServletHttpRequestHandler} instance mapped
|
||||
* to {@code "/**"}; or {@code null} if default servlet handling was not
|
||||
* been enabled.
|
||||
* @since 4.3.12
|
||||
*/
|
||||
@Nullable
|
||||
@@ -95,11 +97,8 @@ public class DefaultServletHandlerConfigurer {
|
||||
if (this.handler == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
||||
handlerMapping.setUrlMap(Collections.singletonMap("/**", this.handler));
|
||||
handlerMapping.setOrder(Integer.MAX_VALUE);
|
||||
return handlerMapping;
|
||||
return new SimpleUrlHandlerMapping(Collections.singletonMap("/**", this.handler),
|
||||
Ordered.LOWEST_PRECEDENCE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -177,10 +177,7 @@ public class ResourceHandlerRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
||||
handlerMapping.setOrder(this.order);
|
||||
handlerMapping.setUrlMap(urlMap);
|
||||
return handlerMapping;
|
||||
return new SimpleUrlHandlerMapping(urlMap, this.order);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -125,10 +125,7 @@ public class ViewControllerRegistry {
|
||||
urlMap.put(registration.getUrlPath(), registration.getViewController());
|
||||
}
|
||||
|
||||
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
||||
handlerMapping.setUrlMap(urlMap);
|
||||
handlerMapping.setOrder(this.order);
|
||||
return handlerMapping;
|
||||
return new SimpleUrlHandlerMapping(urlMap, this.order);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2018 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -34,13 +34,14 @@ import org.springframework.util.CollectionUtils;
|
||||
* bean references, e.g. via the map element in XML bean definitions.
|
||||
*
|
||||
* <p>Mappings to bean names can be set via the "mappings" property, in a form
|
||||
* accepted by the {@code java.util.Properties} class, like as follows:<br>
|
||||
* {@code
|
||||
* accepted by the {@code java.util.Properties} class, as follows:
|
||||
*
|
||||
* <pre class="code">
|
||||
* /welcome.html=ticketController
|
||||
* /show.html=ticketController
|
||||
* }<br>
|
||||
* The syntax is {@code PATH=HANDLER_BEAN_NAME}.
|
||||
* If the path doesn't begin with a slash, one is prepended.
|
||||
* /show.html=ticketController</pre>
|
||||
*
|
||||
* <p>The syntax is {@code PATH=HANDLER_BEAN_NAME}. If the path doesn't begin
|
||||
* with a slash, one is prepended.
|
||||
*
|
||||
* <p>Supports direct matches (given "/test" -> registered "/test") and "*"
|
||||
* pattern matches (given "/test" -> registered "/t*"). Note that the default
|
||||
@@ -50,6 +51,7 @@ import org.springframework.util.CollectionUtils;
|
||||
|
||||
* @author Rod Johnson
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @see #setMappings
|
||||
* @see #setUrlMap
|
||||
* @see BeanNameUrlHandlerMapping
|
||||
@@ -59,6 +61,39 @@ public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
|
||||
private final Map<String, Object> urlMap = new LinkedHashMap<>();
|
||||
|
||||
|
||||
/**
|
||||
* Create a {@code SimpleUrlHandlerMapping} with default settings.
|
||||
* @since 5.2
|
||||
*/
|
||||
public SimpleUrlHandlerMapping() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@code SimpleUrlHandlerMapping} using the supplied URL map.
|
||||
* @param urlMap map with URL paths as keys and handler beans (or handler
|
||||
* bean names) as values
|
||||
* @since 5.2
|
||||
* @see #setUrlMap(Map)
|
||||
*/
|
||||
public SimpleUrlHandlerMapping(Map<String, ?> urlMap) {
|
||||
setUrlMap(urlMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@code SimpleUrlHandlerMapping} using the supplied URL map and order.
|
||||
* @param urlMap map with URL paths as keys and handler beans (or handler
|
||||
* bean names) as values
|
||||
* @param order the order value for this {@code SimpleUrlHandlerMapping}
|
||||
* @since 5.2
|
||||
* @see #setUrlMap(Map)
|
||||
* @see #setOrder(int)
|
||||
*/
|
||||
public SimpleUrlHandlerMapping(Map<String, ?> urlMap, int order) {
|
||||
setUrlMap(urlMap);
|
||||
setOrder(order);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Map URL paths to handler bean names.
|
||||
* This is the typical way of configuring this HandlerMapping.
|
||||
|
||||
@@ -16,8 +16,7 @@
|
||||
|
||||
package org.springframework.web.servlet.handler;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -72,12 +71,10 @@ public class SimpleUrlHandlerMappingTests {
|
||||
|
||||
@Test
|
||||
public void testNewlineInRequest() throws Exception {
|
||||
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
||||
handlerMapping.setUrlDecode(false);
|
||||
Object controller = new Object();
|
||||
Map<String, Object> urlMap = new LinkedHashMap<>();
|
||||
urlMap.put("/*/baz", controller);
|
||||
handlerMapping.setUrlMap(urlMap);
|
||||
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(
|
||||
Collections.singletonMap("/*/baz", controller));
|
||||
handlerMapping.setUrlDecode(false);
|
||||
handlerMapping.setApplicationContext(new StaticApplicationContext());
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo%0a%0dbar/baz");
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.web.servlet.resource;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -174,12 +175,8 @@ public class ResourceUrlProviderTests {
|
||||
|
||||
@Bean
|
||||
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
|
||||
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
|
||||
HashMap<String, ResourceHttpRequestHandler> handlerMap = new HashMap<>();
|
||||
handlerMap.put("/resources/**", handler);
|
||||
SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
|
||||
hm.setUrlMap(handlerMap);
|
||||
return hm;
|
||||
return new SimpleUrlHandlerMapping(
|
||||
Collections.singletonMap("/resources/**", new ResourceHttpRequestHandler()));
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
Reference in New Issue
Block a user