public class MvcUriComponentsBuilder extends UriComponentsBuilder
| Modifier and Type | Class and Description |
|---|---|
static interface |
MvcUriComponentsBuilder.MethodInvocationInfo |
| Modifier and Type | Field and Description |
|---|---|
static String |
MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME
Well-known name for the
CompositeUriComponentsContributor object in the bean factory. |
| Constructor and Description |
|---|
MvcUriComponentsBuilder() |
| Modifier and Type | Method and Description |
|---|---|
static <T> T |
controller(Class<T> controllerType)
Return a "mock" controller instance.
|
static UriComponentsBuilder |
fromController(Class<?> controllerType)
Create a
UriComponentsBuilder by pointing to a controller class. |
static UriComponentsBuilder |
fromMethod(Method method,
Object... argumentValues)
Create a
UriComponentsBuilder by pointing to a controller method and
providing method argument values. |
static UriComponentsBuilder |
fromMethodCall(Object methodInvocationInfo)
Create a
UriComponents by invoking a method on a "mock" controller, similar
to how test frameworks provide mock objects and record method invocations. |
static UriComponentsBuilder |
fromMethodName(Class<?> controllerType,
String methodName,
Object... argumentValues)
Create a
UriComponentsBuilder by pointing to a controller method and
providing method argument values. |
protected static CompositeUriComponentsContributor |
getConfiguredUriComponentsContributor() |
static <T> T |
on(Class<T> controllerType)
Return a "mock" controller instance.
|
build, build, buildAndExpand, buildAndExpand, fragment, fromHttpUrl, fromPath, fromUri, fromUriString, host, newInstance, path, pathSegment, port, query, queryParam, queryParams, replacePath, replaceQuery, replaceQueryParam, scheme, schemeSpecificPart, uri, uriComponents, userInfopublic static final String MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME
CompositeUriComponentsContributor object in the bean factory.public static UriComponentsBuilder fromController(Class<?> controllerType)
UriComponentsBuilder by pointing to a controller class. The
resulting builder contains the current request information up to and including
the Servlet mapping plus any type-level request mapping. If the controller
contains multiple mappings, the first one is used.controllerType - the controller to create a URI forpublic static UriComponentsBuilder fromMethodName(Class<?> controllerType, String methodName, Object... argumentValues)
UriComponentsBuilder by pointing to a controller method and
providing method argument values. The method is matched based on the provided
method name and the number of argument values. If that results in a clash
(i.e. overloaded methods with the same number of parameters), use
fromMethod(java.lang.reflect.Method, Object...) instead.
The argument values are used to prepare the URI for example expanding path variables, or adding query parameters. Any other arguments not relevant to the URI can be provided as null and will be ignored.
Additional (custom) argument types can be supported through an implementation
of UriComponentsContributor.
controllerType - the target controller typemethodName - the target method nameargumentValues - argument values matching to method parameterspublic static UriComponentsBuilder fromMethod(Method method, Object... argumentValues)
UriComponentsBuilder by pointing to a controller method and
providing method argument values. The method argument values are used to
prepare the URI for example expanding path variables, or adding request
parameters. Any other arguments not relevant to the URL can be provided as
null and will be ignored.
Additional (custom) argument types can be supported through an implementation
of UriComponentsContributor.
method - the target controller methodargumentValues - argument values matching to method parameterspublic static UriComponentsBuilder fromMethodCall(Object methodInvocationInfo)
UriComponents by invoking a method on a "mock" controller, similar
to how test frameworks provide mock objects and record method invocations.
For example given this controller:
@RequestMapping("/people/{id}/addresses")
class AddressController {
@RequestMapping("/{country}")
public HttpEntity getAddressesForCountry(@PathVariable String country) { … }
@RequestMapping(value="/", method=RequestMethod.POST)
public void addAddress(Address address) { … }
}
A "mock" controller can be used as follows:
// Inline style with static import of MvcUriComponentsBuilder.mock
MvcUriComponentsBuilder.fromMethodCall(
mock(CustomerController.class).showAddresses("US")).buildAndExpand(1);
// Longer style for preparing multiple URIs and for void controller methods
CustomerController controller = MvcUriComponentsBuilder.mock(CustomController.class);
controller.addAddress(null);
MvcUriComponentsBuilder.fromMethodCall(controller);
The above supports and @RequestParam method parameters.
Any other arguments can be provided as null and will be ignored.
Additional (custom) argument types can be supported through an implementation
of UriComponentsContributor.
methodInvocationInfo - either the value returned from a "mock" controller
invocation or the "mock" controller itself after an invocationprotected static CompositeUriComponentsContributor getConfiguredUriComponentsContributor()
public static <T> T on(Class<T> controllerType)
@RequestMapping method
on the controller is invoked, the supplied argument values are remembered
and the result can then be used to prepare a URL to the method via
fromMethodCall(Object).
This is a shorthand version of controller(Class) intended for
inline use as follows:
UriComponentsBuilder builder = MvcUriComponentsBuilder.fromMethodCall(
on(FooController.class).getFoo(1)).build();
controllerType - the target controllerpublic static <T> T controller(Class<T> controllerType)
@RequestMapping method
on the controller is invoked, the supplied argument values are remembered
and the result can then be used to prepare a URL to the method via
fromMethodCall(Object).
This is a longer version of on(Class) for use with void controller
methods as well as for creating multiple links in succession.
FooController fooController = controller(FooController.class); fooController.saveFoo(1, null); builder = MvcUriComponentsBuilder.fromMethodCall(fooController); fooController.saveFoo(2, null); builder = MvcUriComponentsBuilder.fromMethodCall(fooController);
controllerType - the target controller