Improve expanding in MvcUriComponentsBuilder

Before this change MvcUriComponentsBuilder could not create a
UriComponentsBuilder for methods where the mapping has a URI variable
and no matching method argument for it.

For example a URI variable may be in the type-level mapping but not
all methods may have an @PathVariable argument for it.

This fix addresses the shortcoming such that MvcUriComponentsBuilder
expands the method argument values available to it and leaves remaining
URI variables to be further expanded via UriComponents.expand().

Issue: SPR-11391
This commit is contained in:
Rossen Stoyanchev
2014-02-06 16:46:28 -05:00
parent bdb742b8db
commit 42d0470d94
4 changed files with 57 additions and 11 deletions

View File

@@ -34,6 +34,7 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import java.util.Arrays;
import java.util.List;
@@ -134,6 +135,15 @@ public class MvcUriComponentsBuilderTests {
assertThat(queryParams.get("offset"), contains("10"));
}
// SPR-11391
@Test
public void testFromMethodNameTypeLevelPathVariableWithoutArgumentValue() throws Exception {
UriComponents uriComponents = fromMethodName(UserContactController.class, "showCreate", 123).build();
assertThat(uriComponents.getPath(), is("/user/123/contacts/create"));
}
@Test
public void testFromMethodNameNotMapped() throws Exception {
UriComponents uriComponents = fromMethodName(UnmappedController.class, "unmappedMethod").build();
@@ -290,4 +300,13 @@ public class MvcUriComponentsBuilderTests {
}
}
@RequestMapping("/user/{userId}/contacts")
static class UserContactController {
@RequestMapping("/create")
public String showCreate(@PathVariable Integer userId) {
return null;
}
}
}