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

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