Skip to content

Commit f53cdb8

Browse files
committed
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
1 parent 0a822dd commit f53cdb8

File tree

12 files changed

+113
-76
lines changed

12 files changed

+113
-76
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistry.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,7 @@ protected AbstractUrlHandlerMapping getHandlerMapping() {
152152
urlMap.put(pathPattern, handler);
153153
}
154154
}
155-
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
156-
handlerMapping.setOrder(this.order);
157-
handlerMapping.setUrlMap(urlMap);
158-
return handlerMapping;
155+
return new SimpleUrlHandlerMapping(urlMap, this.order);
159156
}
160157

161158
}

spring-webflux/src/main/java/org/springframework/web/reactive/handler/SimpleUrlHandlerMapping.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,12 +32,11 @@
3232
* <p>The "urlMap" property is suitable for populating the handler map with
3333
* bean instances. Mappings to bean names can be set via the "mappings"
3434
* property, in a form accepted by the {@code java.util.Properties} class,
35-
* like as follows:
35+
* as follows:
3636
*
37-
* <pre>
37+
* <pre class="code">
3838
* /welcome.html=ticketController
39-
* /show.html=ticketController
40-
* </pre>
39+
* /show.html=ticketController</pre>
4140
*
4241
* <p>The syntax is {@code PATH=HANDLER_BEAN_NAME}. If the path doesn't begin
4342
* with a slash, one is prepended.
@@ -49,13 +48,47 @@
4948
* {@link org.springframework.web.util.pattern.PathPattern} javadoc.
5049
*
5150
* @author Rossen Stoyanchev
51+
* @author Sam Brannen
5252
* @since 5.0
5353
*/
5454
public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
5555

5656
private final Map<String, Object> urlMap = new LinkedHashMap<>();
5757

5858

59+
/**
60+
* Create a {@code SimpleUrlHandlerMapping} with default settings.
61+
* @since 5.2
62+
*/
63+
public SimpleUrlHandlerMapping() {
64+
}
65+
66+
/**
67+
* Create a {@code SimpleUrlHandlerMapping} using the supplied URL map.
68+
* @param urlMap map with URL paths as keys and handler beans (or handler
69+
* bean names) as values
70+
* @since 5.2
71+
* @see #setUrlMap(Map)
72+
*/
73+
public SimpleUrlHandlerMapping(Map<String, ?> urlMap) {
74+
setUrlMap(urlMap);
75+
}
76+
77+
/**
78+
* Create a {@code SimpleUrlHandlerMapping} using the supplied URL map and order.
79+
* @param urlMap map with URL paths as keys and handler beans (or handler
80+
* bean names) as values
81+
* @param order the order value for this {@code SimpleUrlHandlerMapping}
82+
* @since 5.2
83+
* @see #setUrlMap(Map)
84+
* @see #setOrder(int)
85+
*/
86+
public SimpleUrlHandlerMapping(Map<String, ?> urlMap, int order) {
87+
setUrlMap(urlMap);
88+
setOrder(order);
89+
}
90+
91+
5992
/**
6093
* Map URL paths to handler bean names.
6194
* This is the typical way of configuring this HandlerMapping.

spring-webflux/src/test/java/org/springframework/web/reactive/resource/ResourceUrlProviderTests.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,7 @@ static class HandlerMappingConfiguration {
152152

153153
@Bean
154154
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
155-
ResourceWebHandler handler = new ResourceWebHandler();
156-
HashMap<String, ResourceWebHandler> handlerMap = new HashMap<>();
157-
handlerMap.put("/resources/**", handler);
158-
SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
159-
hm.setUrlMap(handlerMap);
160-
return hm;
155+
return new SimpleUrlHandlerMapping(Collections.singletonMap("/resources/**", new ResourceWebHandler()));
161156
}
162157

163158
@Bean

spring-webflux/src/test/java/org/springframework/web/reactive/result/SimpleUrlHandlerMappingIntegrationTests.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,16 @@ static class WebConfig {
119119

120120
@Bean
121121
public SimpleUrlHandlerMapping handlerMapping() {
122-
return new SimpleUrlHandlerMapping() {
123-
{
124-
Map<String, Object> map = new HashMap<>();
125-
map.put("/foo", (WebHandler) exchange ->
126-
exchange.getResponse().writeWith(Flux.just(asDataBuffer("foo"))));
127-
map.put("/bar", (WebHandler) exchange ->
128-
exchange.getResponse().writeWith(Flux.just(asDataBuffer("bar"))));
129-
map.put("/header", (WebHandler) exchange -> {
130-
exchange.getResponse().getHeaders().add("foo", "bar");
131-
return Mono.empty();
132-
});
133-
setUrlMap(map);
134-
}
135-
};
122+
Map<String, Object> map = new HashMap<>();
123+
map.put("/foo", (WebHandler) exchange ->
124+
exchange.getResponse().writeWith(Flux.just(asDataBuffer("foo"))));
125+
map.put("/bar", (WebHandler) exchange ->
126+
exchange.getResponse().writeWith(Flux.just(asDataBuffer("bar"))));
127+
map.put("/header", (WebHandler) exchange -> {
128+
exchange.getResponse().getHeaders().add("foo", "bar");
129+
return Mono.empty();
130+
});
131+
return new SimpleUrlHandlerMapping(map);
136132
}
137133

138134
@Bean

spring-webflux/src/test/java/org/springframework/web/reactive/socket/WebSocketIntegrationTests.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,8 @@ public HandlerMapping handlerMapping() {
145145
map.put("/sub-protocol", new SubProtocolWebSocketHandler());
146146
map.put("/custom-header", new CustomHeaderHandler());
147147
map.put("/close", new SessionClosingHandler());
148-
149-
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
150-
mapping.setUrlMap(map);
151-
return mapping;
148+
return new SimpleUrlHandlerMapping(map);
152149
}
153-
154150
}
155151

156152

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/DefaultServletHandlerConfigurer.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919
import java.util.Collections;
2020
import javax.servlet.ServletContext;
2121

22+
import org.springframework.core.Ordered;
2223
import org.springframework.lang.Nullable;
2324
import org.springframework.util.Assert;
2425
import org.springframework.web.servlet.DispatcherServlet;
@@ -85,21 +86,19 @@ public void enable(@Nullable String defaultServletName) {
8586

8687

8788
/**
88-
* Return a handler mapping instance ordered at {@link Integer#MAX_VALUE} containing the
89-
* {@link DefaultServletHttpRequestHandler} instance mapped to {@code "/**"};
90-
* or {@code null} if default servlet handling was not been enabled.
89+
* Return a handler mapping instance ordered at {@link Ordered#LOWEST_PRECEDENCE}
90+
* containing the {@link DefaultServletHttpRequestHandler} instance mapped
91+
* to {@code "/**"}; or {@code null} if default servlet handling was not
92+
* been enabled.
9193
* @since 4.3.12
9294
*/
9395
@Nullable
9496
protected SimpleUrlHandlerMapping buildHandlerMapping() {
9597
if (this.handler == null) {
9698
return null;
9799
}
98-
99-
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
100-
handlerMapping.setUrlMap(Collections.singletonMap("/**", this.handler));
101-
handlerMapping.setOrder(Integer.MAX_VALUE);
102-
return handlerMapping;
100+
return new SimpleUrlHandlerMapping(Collections.singletonMap("/**", this.handler),
101+
Ordered.LOWEST_PRECEDENCE);
103102
}
104103

105104
}

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -177,10 +177,7 @@ protected AbstractHandlerMapping getHandlerMapping() {
177177
}
178178
}
179179

180-
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
181-
handlerMapping.setOrder(this.order);
182-
handlerMapping.setUrlMap(urlMap);
183-
return handlerMapping;
180+
return new SimpleUrlHandlerMapping(urlMap, this.order);
184181
}
185182

186183
}

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ViewControllerRegistry.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,7 @@ protected SimpleUrlHandlerMapping buildHandlerMapping() {
125125
urlMap.put(registration.getUrlPath(), registration.getViewController());
126126
}
127127

128-
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
129-
handlerMapping.setUrlMap(urlMap);
130-
handlerMapping.setOrder(this.order);
131-
return handlerMapping;
128+
return new SimpleUrlHandlerMapping(urlMap, this.order);
132129
}
133130

134131
}

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMapping.java

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -34,13 +34,14 @@
3434
* bean references, e.g. via the map element in XML bean definitions.
3535
*
3636
* <p>Mappings to bean names can be set via the "mappings" property, in a form
37-
* accepted by the {@code java.util.Properties} class, like as follows:<br>
38-
* {@code
37+
* accepted by the {@code java.util.Properties} class, as follows:
38+
*
39+
* <pre class="code">
3940
* /welcome.html=ticketController
40-
* /show.html=ticketController
41-
* }<br>
42-
* The syntax is {@code PATH=HANDLER_BEAN_NAME}.
43-
* If the path doesn't begin with a slash, one is prepended.
41+
* /show.html=ticketController</pre>
42+
*
43+
* <p>The syntax is {@code PATH=HANDLER_BEAN_NAME}. If the path doesn't begin
44+
* with a slash, one is prepended.
4445
*
4546
* <p>Supports direct matches (given "/test" -&gt; registered "/test") and "*"
4647
* pattern matches (given "/test" -&gt; registered "/t*"). Note that the default
@@ -50,6 +51,7 @@
5051
5152
* @author Rod Johnson
5253
* @author Juergen Hoeller
54+
* @author Sam Brannen
5355
* @see #setMappings
5456
* @see #setUrlMap
5557
* @see BeanNameUrlHandlerMapping
@@ -59,6 +61,39 @@ public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
5961
private final Map<String, Object> urlMap = new LinkedHashMap<>();
6062

6163

64+
/**
65+
* Create a {@code SimpleUrlHandlerMapping} with default settings.
66+
* @since 5.2
67+
*/
68+
public SimpleUrlHandlerMapping() {
69+
}
70+
71+
/**
72+
* Create a {@code SimpleUrlHandlerMapping} using the supplied URL map.
73+
* @param urlMap map with URL paths as keys and handler beans (or handler
74+
* bean names) as values
75+
* @since 5.2
76+
* @see #setUrlMap(Map)
77+
*/
78+
public SimpleUrlHandlerMapping(Map<String, ?> urlMap) {
79+
setUrlMap(urlMap);
80+
}
81+
82+
/**
83+
* Create a {@code SimpleUrlHandlerMapping} using the supplied URL map and order.
84+
* @param urlMap map with URL paths as keys and handler beans (or handler
85+
* bean names) as values
86+
* @param order the order value for this {@code SimpleUrlHandlerMapping}
87+
* @since 5.2
88+
* @see #setUrlMap(Map)
89+
* @see #setOrder(int)
90+
*/
91+
public SimpleUrlHandlerMapping(Map<String, ?> urlMap, int order) {
92+
setUrlMap(urlMap);
93+
setOrder(order);
94+
}
95+
96+
6297
/**
6398
* Map URL paths to handler bean names.
6499
* This is the typical way of configuring this HandlerMapping.

spring-webmvc/src/test/java/org/springframework/web/servlet/handler/SimpleUrlHandlerMappingTests.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.web.servlet.handler;
1818

19-
import java.util.LinkedHashMap;
20-
import java.util.Map;
19+
import java.util.Collections;
2120

2221
import org.junit.Test;
2322

@@ -72,12 +71,10 @@ public void urlMappingWithProps() throws Exception {
7271

7372
@Test
7473
public void testNewlineInRequest() throws Exception {
75-
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
76-
handlerMapping.setUrlDecode(false);
7774
Object controller = new Object();
78-
Map<String, Object> urlMap = new LinkedHashMap<>();
79-
urlMap.put("/*/baz", controller);
80-
handlerMapping.setUrlMap(urlMap);
75+
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(
76+
Collections.singletonMap("/*/baz", controller));
77+
handlerMapping.setUrlDecode(false);
8178
handlerMapping.setApplicationContext(new StaticApplicationContext());
8279

8380
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo%0a%0dbar/baz");

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlProviderTests.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.web.servlet.resource;
1818

1919
import java.util.ArrayList;
20+
import java.util.Collections;
2021
import java.util.HashMap;
2122
import java.util.List;
2223
import java.util.Map;
@@ -174,12 +175,8 @@ static class HandlerMappingConfiguration {
174175

175176
@Bean
176177
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
177-
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
178-
HashMap<String, ResourceHttpRequestHandler> handlerMap = new HashMap<>();
179-
handlerMap.put("/resources/**", handler);
180-
SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
181-
hm.setUrlMap(handlerMap);
182-
return hm;
178+
return new SimpleUrlHandlerMapping(
179+
Collections.singletonMap("/resources/**", new ResourceHttpRequestHandler()));
183180
}
184181

185182
@Bean

src/docs/asciidoc/web/webflux-websocket.adoc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ Then you can map it to a URL and add a `WebSocketHandlerAdapter`, as the followi
5353
public HandlerMapping handlerMapping() {
5454
Map<String, WebSocketHandler> map = new HashMap<>();
5555
map.put("/path", new MyWebSocketHandler());
56+
int order = -1; // before annotated controllers
5657
57-
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
58-
mapping.setUrlMap(map);
59-
mapping.setOrder(-1); // before annotated controllers
60-
return mapping;
58+
return new SimpleUrlHandlerMapping(map, order);
6159
}
6260
6361
@Bean

0 commit comments

Comments
 (0)