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:
parent
0a822ddf2d
commit
f53cdb8bd2
|
|
@ -152,10 +152,7 @@ public class ResourceHandlerRegistry {
|
||||||
urlMap.put(pathPattern, handler);
|
urlMap.put(pathPattern, handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
return new SimpleUrlHandlerMapping(urlMap, this.order);
|
||||||
handlerMapping.setOrder(this.order);
|
|
||||||
handlerMapping.setUrlMap(urlMap);
|
|
||||||
return handlerMapping;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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
|
* <p>The "urlMap" property is suitable for populating the handler map with
|
||||||
* bean instances. Mappings to bean names can be set via the "mappings"
|
* bean instances. Mappings to bean names can be set via the "mappings"
|
||||||
* property, in a form accepted by the {@code java.util.Properties} class,
|
* property, in a form accepted by the {@code java.util.Properties} class,
|
||||||
* like as follows:
|
* as follows:
|
||||||
*
|
*
|
||||||
* <pre>
|
* <pre class="code">
|
||||||
* /welcome.html=ticketController
|
* /welcome.html=ticketController
|
||||||
* /show.html=ticketController
|
* /show.html=ticketController</pre>
|
||||||
* </pre>
|
|
||||||
*
|
*
|
||||||
* <p>The syntax is {@code PATH=HANDLER_BEAN_NAME}. If the path doesn't begin
|
* <p>The syntax is {@code PATH=HANDLER_BEAN_NAME}. If the path doesn't begin
|
||||||
* with a slash, one is prepended.
|
* with a slash, one is prepended.
|
||||||
|
|
@ -49,6 +48,7 @@ import org.springframework.util.CollectionUtils;
|
||||||
* {@link org.springframework.web.util.pattern.PathPattern} javadoc.
|
* {@link org.springframework.web.util.pattern.PathPattern} javadoc.
|
||||||
*
|
*
|
||||||
* @author Rossen Stoyanchev
|
* @author Rossen Stoyanchev
|
||||||
|
* @author Sam Brannen
|
||||||
* @since 5.0
|
* @since 5.0
|
||||||
*/
|
*/
|
||||||
public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
|
public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
|
||||||
|
|
@ -56,6 +56,39 @@ public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
|
||||||
private final Map<String, Object> urlMap = new LinkedHashMap<>();
|
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.
|
* Map URL paths to handler bean names.
|
||||||
* This is the typical way of configuring this HandlerMapping.
|
* This is the typical way of configuring this HandlerMapping.
|
||||||
|
|
|
||||||
|
|
@ -152,12 +152,7 @@ public class ResourceUrlProviderTests {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
|
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
|
||||||
ResourceWebHandler handler = new ResourceWebHandler();
|
return new SimpleUrlHandlerMapping(Collections.singletonMap("/resources/**", new ResourceWebHandler()));
|
||||||
HashMap<String, ResourceWebHandler> handlerMap = new HashMap<>();
|
|
||||||
handlerMap.put("/resources/**", handler);
|
|
||||||
SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
|
|
||||||
hm.setUrlMap(handlerMap);
|
|
||||||
return hm;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
||||||
|
|
@ -119,20 +119,16 @@ public class SimpleUrlHandlerMappingIntegrationTests extends AbstractHttpHandler
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SimpleUrlHandlerMapping handlerMapping() {
|
public SimpleUrlHandlerMapping handlerMapping() {
|
||||||
return new SimpleUrlHandlerMapping() {
|
Map<String, Object> map = new HashMap<>();
|
||||||
{
|
map.put("/foo", (WebHandler) exchange ->
|
||||||
Map<String, Object> map = new HashMap<>();
|
exchange.getResponse().writeWith(Flux.just(asDataBuffer("foo"))));
|
||||||
map.put("/foo", (WebHandler) exchange ->
|
map.put("/bar", (WebHandler) exchange ->
|
||||||
exchange.getResponse().writeWith(Flux.just(asDataBuffer("foo"))));
|
exchange.getResponse().writeWith(Flux.just(asDataBuffer("bar"))));
|
||||||
map.put("/bar", (WebHandler) exchange ->
|
map.put("/header", (WebHandler) exchange -> {
|
||||||
exchange.getResponse().writeWith(Flux.just(asDataBuffer("bar"))));
|
exchange.getResponse().getHeaders().add("foo", "bar");
|
||||||
map.put("/header", (WebHandler) exchange -> {
|
return Mono.empty();
|
||||||
exchange.getResponse().getHeaders().add("foo", "bar");
|
});
|
||||||
return Mono.empty();
|
return new SimpleUrlHandlerMapping(map);
|
||||||
});
|
|
||||||
setUrlMap(map);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
||||||
|
|
@ -145,12 +145,8 @@ public class WebSocketIntegrationTests extends AbstractWebSocketIntegrationTests
|
||||||
map.put("/sub-protocol", new SubProtocolWebSocketHandler());
|
map.put("/sub-protocol", new SubProtocolWebSocketHandler());
|
||||||
map.put("/custom-header", new CustomHeaderHandler());
|
map.put("/custom-header", new CustomHeaderHandler());
|
||||||
map.put("/close", new SessionClosingHandler());
|
map.put("/close", new SessionClosingHandler());
|
||||||
|
return new SimpleUrlHandlerMapping(map);
|
||||||
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
|
|
||||||
mapping.setUrlMap(map);
|
|
||||||
return mapping;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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 java.util.Collections;
|
||||||
import javax.servlet.ServletContext;
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
|
import org.springframework.core.Ordered;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.web.servlet.DispatcherServlet;
|
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
|
* Return a handler mapping instance ordered at {@link Ordered#LOWEST_PRECEDENCE}
|
||||||
* {@link DefaultServletHttpRequestHandler} instance mapped to {@code "/**"};
|
* containing the {@link DefaultServletHttpRequestHandler} instance mapped
|
||||||
* or {@code null} if default servlet handling was not been enabled.
|
* to {@code "/**"}; or {@code null} if default servlet handling was not
|
||||||
|
* been enabled.
|
||||||
* @since 4.3.12
|
* @since 4.3.12
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|
@ -95,11 +97,8 @@ public class DefaultServletHandlerConfigurer {
|
||||||
if (this.handler == null) {
|
if (this.handler == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
return new SimpleUrlHandlerMapping(Collections.singletonMap("/**", this.handler),
|
||||||
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
Ordered.LOWEST_PRECEDENCE);
|
||||||
handlerMapping.setUrlMap(Collections.singletonMap("/**", this.handler));
|
|
||||||
handlerMapping.setOrder(Integer.MAX_VALUE);
|
|
||||||
return handlerMapping;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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();
|
return new SimpleUrlHandlerMapping(urlMap, this.order);
|
||||||
handlerMapping.setOrder(this.order);
|
|
||||||
handlerMapping.setUrlMap(urlMap);
|
|
||||||
return handlerMapping;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -125,10 +125,7 @@ public class ViewControllerRegistry {
|
||||||
urlMap.put(registration.getUrlPath(), registration.getViewController());
|
urlMap.put(registration.getUrlPath(), registration.getViewController());
|
||||||
}
|
}
|
||||||
|
|
||||||
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
return new SimpleUrlHandlerMapping(urlMap, this.order);
|
||||||
handlerMapping.setUrlMap(urlMap);
|
|
||||||
handlerMapping.setOrder(this.order);
|
|
||||||
return handlerMapping;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with 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.
|
* 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
|
* <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>
|
* accepted by the {@code java.util.Properties} class, as follows:
|
||||||
* {@code
|
*
|
||||||
|
* <pre class="code">
|
||||||
* /welcome.html=ticketController
|
* /welcome.html=ticketController
|
||||||
* /show.html=ticketController
|
* /show.html=ticketController</pre>
|
||||||
* }<br>
|
*
|
||||||
* The syntax is {@code PATH=HANDLER_BEAN_NAME}.
|
* <p>The syntax is {@code PATH=HANDLER_BEAN_NAME}. If the path doesn't begin
|
||||||
* If the path doesn't begin with a slash, one is prepended.
|
* with a slash, one is prepended.
|
||||||
*
|
*
|
||||||
* <p>Supports direct matches (given "/test" -> registered "/test") and "*"
|
* <p>Supports direct matches (given "/test" -> registered "/test") and "*"
|
||||||
* pattern matches (given "/test" -> registered "/t*"). Note that the default
|
* pattern matches (given "/test" -> registered "/t*"). Note that the default
|
||||||
|
|
@ -50,6 +51,7 @@ import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
* @author Rod Johnson
|
* @author Rod Johnson
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
|
* @author Sam Brannen
|
||||||
* @see #setMappings
|
* @see #setMappings
|
||||||
* @see #setUrlMap
|
* @see #setUrlMap
|
||||||
* @see BeanNameUrlHandlerMapping
|
* @see BeanNameUrlHandlerMapping
|
||||||
|
|
@ -59,6 +61,39 @@ public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
|
||||||
private final Map<String, Object> urlMap = new LinkedHashMap<>();
|
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.
|
* Map URL paths to handler bean names.
|
||||||
* This is the typical way of configuring this HandlerMapping.
|
* This is the typical way of configuring this HandlerMapping.
|
||||||
|
|
|
||||||
|
|
@ -16,8 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.web.servlet.handler;
|
package org.springframework.web.servlet.handler;
|
||||||
|
|
||||||
import java.util.LinkedHashMap;
|
import java.util.Collections;
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
|
@ -72,12 +71,10 @@ public class SimpleUrlHandlerMappingTests {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testNewlineInRequest() throws Exception {
|
public void testNewlineInRequest() throws Exception {
|
||||||
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping();
|
|
||||||
handlerMapping.setUrlDecode(false);
|
|
||||||
Object controller = new Object();
|
Object controller = new Object();
|
||||||
Map<String, Object> urlMap = new LinkedHashMap<>();
|
SimpleUrlHandlerMapping handlerMapping = new SimpleUrlHandlerMapping(
|
||||||
urlMap.put("/*/baz", controller);
|
Collections.singletonMap("/*/baz", controller));
|
||||||
handlerMapping.setUrlMap(urlMap);
|
handlerMapping.setUrlDecode(false);
|
||||||
handlerMapping.setApplicationContext(new StaticApplicationContext());
|
handlerMapping.setApplicationContext(new StaticApplicationContext());
|
||||||
|
|
||||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo%0a%0dbar/baz");
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo%0a%0dbar/baz");
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
package org.springframework.web.servlet.resource;
|
package org.springframework.web.servlet.resource;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
@ -174,12 +175,8 @@ public class ResourceUrlProviderTests {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
|
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
|
||||||
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
|
return new SimpleUrlHandlerMapping(
|
||||||
HashMap<String, ResourceHttpRequestHandler> handlerMap = new HashMap<>();
|
Collections.singletonMap("/resources/**", new ResourceHttpRequestHandler()));
|
||||||
handlerMap.put("/resources/**", handler);
|
|
||||||
SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
|
|
||||||
hm.setUrlMap(handlerMap);
|
|
||||||
return hm;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
||||||
|
|
@ -53,11 +53,9 @@ Then you can map it to a URL and add a `WebSocketHandlerAdapter`, as the followi
|
||||||
public HandlerMapping handlerMapping() {
|
public HandlerMapping handlerMapping() {
|
||||||
Map<String, WebSocketHandler> map = new HashMap<>();
|
Map<String, WebSocketHandler> map = new HashMap<>();
|
||||||
map.put("/path", new MyWebSocketHandler());
|
map.put("/path", new MyWebSocketHandler());
|
||||||
|
int order = -1; // before annotated controllers
|
||||||
|
|
||||||
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
|
return new SimpleUrlHandlerMapping(map, order);
|
||||||
mapping.setUrlMap(map);
|
|
||||||
mapping.setOrder(-1); // before annotated controllers
|
|
||||||
return mapping;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue