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:
Sam Brannen 2019-07-28 17:50:44 +02:00
parent 0a822ddf2d
commit f53cdb8bd2
12 changed files with 113 additions and 76 deletions

View File

@ -152,10 +152,7 @@ public class ResourceHandlerRegistry {
urlMap.put(pathPattern, handler);
}
}
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
handlerMapping.setOrder(this.order);
handlerMapping.setUrlMap(urlMap);
return handlerMapping;
return new SimpleUrlHandlerMapping(urlMap, this.order);
}
}

View File

@ -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.
@ -32,12 +32,11 @@ import org.springframework.util.CollectionUtils;
* <p>The "urlMap" property is suitable for populating the handler map with
* bean instances. 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:
* as follows:
*
* <pre>
* <pre class="code">
* /welcome.html=ticketController
* /show.html=ticketController
* </pre>
* /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.
@ -49,6 +48,7 @@ import org.springframework.util.CollectionUtils;
* {@link org.springframework.web.util.pattern.PathPattern} javadoc.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
* @since 5.0
*/
public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
@ -56,6 +56,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.

View File

@ -152,12 +152,7 @@ public class ResourceUrlProviderTests {
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
ResourceWebHandler handler = new ResourceWebHandler();
HashMap<String, ResourceWebHandler> handlerMap = new HashMap<>();
handlerMap.put("/resources/**", handler);
SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
hm.setUrlMap(handlerMap);
return hm;
return new SimpleUrlHandlerMapping(Collections.singletonMap("/resources/**", new ResourceWebHandler()));
}
@Bean

View File

@ -119,20 +119,16 @@ public class SimpleUrlHandlerMappingIntegrationTests extends AbstractHttpHandler
@Bean
public SimpleUrlHandlerMapping handlerMapping() {
return new SimpleUrlHandlerMapping() {
{
Map<String, Object> map = new HashMap<>();
map.put("/foo", (WebHandler) exchange ->
exchange.getResponse().writeWith(Flux.just(asDataBuffer("foo"))));
map.put("/bar", (WebHandler) exchange ->
exchange.getResponse().writeWith(Flux.just(asDataBuffer("bar"))));
map.put("/header", (WebHandler) exchange -> {
exchange.getResponse().getHeaders().add("foo", "bar");
return Mono.empty();
});
setUrlMap(map);
}
};
Map<String, Object> map = new HashMap<>();
map.put("/foo", (WebHandler) exchange ->
exchange.getResponse().writeWith(Flux.just(asDataBuffer("foo"))));
map.put("/bar", (WebHandler) exchange ->
exchange.getResponse().writeWith(Flux.just(asDataBuffer("bar"))));
map.put("/header", (WebHandler) exchange -> {
exchange.getResponse().getHeaders().add("foo", "bar");
return Mono.empty();
});
return new SimpleUrlHandlerMapping(map);
}
@Bean

View File

@ -145,12 +145,8 @@ public class WebSocketIntegrationTests extends AbstractWebSocketIntegrationTests
map.put("/sub-protocol", new SubProtocolWebSocketHandler());
map.put("/custom-header", new CustomHeaderHandler());
map.put("/close", new SessionClosingHandler());
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setUrlMap(map);
return mapping;
return new SimpleUrlHandlerMapping(map);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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" -&gt; registered "/test") and "*"
* pattern matches (given "/test" -&gt; 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.

View File

@ -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");

View File

@ -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

View File

@ -53,11 +53,9 @@ Then you can map it to a URL and add a `WebSocketHandlerAdapter`, as the followi
public HandlerMapping handlerMapping() {
Map<String, WebSocketHandler> map = new HashMap<>();
map.put("/path", new MyWebSocketHandler());
int order = -1; // before annotated controllers
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setUrlMap(map);
mapping.setOrder(-1); // before annotated controllers
return mapping;
return new SimpleUrlHandlerMapping(map, order);
}
@Bean