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