SPR-8020 Support UriComponentsBuilder as a controller method argument.
The UriComponentsBuilder instance passed into the method is initialized with current request information including host, scheme, port, context path, and the servlet mapping's literal part. Also added shortcut methods to buildAndExpand in UriComponentsBuilder.
This commit is contained in:
@@ -130,6 +130,9 @@ import java.lang.annotation.Target;
|
||||
* for marking form processing as complete (triggering the cleanup of session
|
||||
* attributes that have been indicated by the {@link SessionAttributes} annotation
|
||||
* at the handler type level).
|
||||
* <li>{@link org.springframework.web.util.UriComponentsBuilder} a builder for
|
||||
* preparing a URL relative to the current request's host, port, scheme, context
|
||||
* path, and the literal part of the servlet mapping.
|
||||
* </ul>
|
||||
*
|
||||
* <p>The following return types are supported for handler methods:
|
||||
|
||||
@@ -20,6 +20,7 @@ import java.net.URI;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@@ -221,6 +222,30 @@ public class UriComponentsBuilder {
|
||||
return new UriComponents(scheme, userInfo, host, port, pathBuilder.build(), queryParams, fragment, encoded, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a {@code UriComponents} instance and replaces URI template variables
|
||||
* with the values from a map. This is a shortcut method, which combines
|
||||
* calls to {@link #build()} and then {@link UriComponents#expand(Map)}.
|
||||
*
|
||||
* @param uriVariables the map of URI variables
|
||||
* @return the URI components with expanded values
|
||||
*/
|
||||
public UriComponents buildAndExpand(Map<String, ?> uriVariables) {
|
||||
return build(false).expand(uriVariables);
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a {@code UriComponents} instance and replaces URI template variables
|
||||
* with the values from an array. This is a shortcut method, which combines
|
||||
* calls to {@link #build()} and then {@link UriComponents#expand(Object...)}.
|
||||
*
|
||||
* @param uriVariableValues URI variable values
|
||||
* @return the URI components with expanded values
|
||||
*/
|
||||
public UriComponents buildAndExpand(Object... uriVariableValues) {
|
||||
return build(false).expand(uriVariableValues);
|
||||
}
|
||||
|
||||
// URI components methods
|
||||
|
||||
/**
|
||||
|
||||
@@ -16,17 +16,19 @@
|
||||
|
||||
package org.springframework.web.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
public class UriComponentsBuilderTests {
|
||||
|
||||
@@ -227,7 +229,6 @@ public class UriComponentsBuilderTests {
|
||||
assertEquals(expectedQueryParams, result.getQueryParams());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void replaceQueryParam() {
|
||||
UriComponentsBuilder builder = UriComponentsBuilder.newInstance().queryParam("baz", "qux", 42);
|
||||
@@ -243,4 +244,15 @@ public class UriComponentsBuilderTests {
|
||||
assertNull("Query param should have been deleted", result.getQuery());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void buildAndExpand() {
|
||||
UriComponents result = UriComponentsBuilder.fromPath("/{foo}").buildAndExpand("fooValue");
|
||||
assertEquals("/fooValue", result.toUriString());
|
||||
|
||||
Map<String, String> values = new HashMap<String, String>();
|
||||
values.put("foo", "fooValue");
|
||||
values.put("bar", "barValue");
|
||||
result = UriComponentsBuilder.fromPath("/{foo}/{bar}").buildAndExpand(values);
|
||||
assertEquals("/fooValue/barValue", result.toUriString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,7 +62,6 @@ public class UriComponentsTests {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void invalidCharacters() {
|
||||
UriComponentsBuilder.fromPath("/{foo}").build(true);
|
||||
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
|
||||
Reference in New Issue
Block a user