Class FrameworkMvcUriComponentsBuilder
FrameworkMapping annotations. Since this class isn't extensible due to heavy usage of private
we had to copy and modify the whole class. Spring version updates should seek to take in changes from MvcUriComponentsBuilder
into this class.
Creates instances of UriComponentsBuilder
by pointing to Spring MVC controllers and @RequestMapping methods.
The static fromXxx(...) methods prepare links relative to the
current request as determined by a call to
ServletUriComponentsBuilder.fromCurrentServletMapping().
The static fromXxx(UriComponentsBuilder,...) methods can be given
the baseUrl when operating outside the context of a request.
You can also create an MvcUriComponentsBuilder instance with a baseUrl
via relativeTo(org.springframework.web.util.UriComponentsBuilder)
and then use the non-static withXxx(...) method variants.
- Since:
- 5.2
- Author:
- Oliver Gierke, Rossen Stoyanchev, Sam Brannen, Philip Baggett (pbaggett)
- See Also:
-
MvcUriComponentsBuilder
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic interface -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringWell-known name for theCompositeUriComponentsContributorobject in the bean factory. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedFrameworkMvcUriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder baseUrl) Default constructor. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tcontroller(Class<T> controllerType) Return a "mock" controller instance.static org.springframework.web.util.UriComponentsBuilderfromController(Class<?> controllerType) Create aUriComponentsBuilderfrom the mapping of a controller class and current request information including Servlet mapping.static org.springframework.web.util.UriComponentsBuilderfromController(org.springframework.web.util.UriComponentsBuilder builder, Class<?> controllerType) An alternative tofromController(Class)that accepts aUriComponentsBuilderrepresenting the base URL.fromMappingName(String mappingName) Create a URL from the name of a Spring MVC controller method's request mapping.fromMappingName(org.springframework.web.util.UriComponentsBuilder builder, String name) An alternative tofromMappingName(String)that accepts aUriComponentsBuilderrepresenting the base URL.static org.springframework.web.util.UriComponentsBuilderfromMethod(Class<?> controllerType, Method method, Object... args) Create aUriComponentsBuilderfrom the mapping of a controller method and an array of method argument values.static org.springframework.web.util.UriComponentsBuilderfromMethod(Method method, Object... args) Deprecated.as of 4.2, this is deprecated in favor of the overloaded method that also accepts a controllerType argumentstatic org.springframework.web.util.UriComponentsBuilderfromMethod(org.springframework.web.util.UriComponentsBuilder baseUrl, Class<?> controllerType, Method method, Object... args) An alternative tofromMethod(Class, Method, Object...)that accepts aUriComponentsBuilderrepresenting the base URL.static org.springframework.web.util.UriComponentsBuilderfromMethodCall(Object info) Create aUriComponentsBuilderby invoking a "mock" controller method.static org.springframework.web.util.UriComponentsBuilderfromMethodCall(org.springframework.web.util.UriComponentsBuilder builder, Object info) An alternative tofromMethodCall(Object)that accepts aUriComponentsBuilderrepresenting the base URL.static org.springframework.web.util.UriComponentsBuilderfromMethodName(Class<?> controllerType, String methodName, Object... args) Create aUriComponentsBuilderfrom the mapping of a controller method and an array of method argument values.static org.springframework.web.util.UriComponentsBuilderfromMethodName(org.springframework.web.util.UriComponentsBuilder builder, Class<?> controllerType, String methodName, Object... args) An alternative tofromMethodName(Class, String, Object...)that accepts aUriComponentsBuilderrepresenting the base URL.static <T> TReturn a "mock" controller instance.relativeTo(org.springframework.web.util.UriComponentsBuilder baseUrl) Create an instance of this class with a base URL.org.springframework.web.util.UriComponentsBuilderwithController(Class<?> controllerType) An alternative tofromController(Class)for use with an instance of this class created via a call torelativeTo(org.springframework.web.util.UriComponentsBuilder).withMappingName(String mappingName) An alternative tofromMappingName(String)for use with an instance of this class created viarelativeTo(org.springframework.web.util.UriComponentsBuilder).org.springframework.web.util.UriComponentsBuilderwithMethod(Class<?> controllerType, Method method, Object... args) An alternative tofromMethod(Class, Method, Object...)for use with an instance of this class created viarelativeTo(org.springframework.web.util.UriComponentsBuilder).org.springframework.web.util.UriComponentsBuilderwithMethodCall(Object invocationInfo) An alternative tofromMethodCall(Object)for use with an instance of this class created viarelativeTo(org.springframework.web.util.UriComponentsBuilder).org.springframework.web.util.UriComponentsBuilderwithMethodName(Class<?> controllerType, String methodName, Object... args) An alternative tofromMethodName(Class, String, Object...)} for use with an instance of this class created viarelativeTo(org.springframework.web.util.UriComponentsBuilder).
-
Field Details
-
MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME
Well-known name for theCompositeUriComponentsContributorobject in the bean factory.- See Also:
-
-
Constructor Details
-
FrameworkMvcUriComponentsBuilder
protected FrameworkMvcUriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder baseUrl) Default constructor. Protected to prevent direct instantiation.
-
-
Method Details
-
relativeTo
public static FrameworkMvcUriComponentsBuilder relativeTo(org.springframework.web.util.UriComponentsBuilder baseUrl) Create an instance of this class with a base URL. After that calls to one of the instance basedwithXxx(...} methods will create URLs relative to the given base URL. -
fromController
public static org.springframework.web.util.UriComponentsBuilder fromController(Class<?> controllerType) Create aUriComponentsBuilderfrom the mapping of a controller class and current request information including Servlet mapping. If the controller contains multiple mappings, only the first one is used.- Parameters:
controllerType- the controller to build a URI for- Returns:
- a UriComponentsBuilder instance (never
null)
-
fromController
public static org.springframework.web.util.UriComponentsBuilder fromController(org.springframework.web.util.UriComponentsBuilder builder, Class<?> controllerType) An alternative tofromController(Class)that accepts aUriComponentsBuilderrepresenting the base URL. This is useful when using MvcUriComponentsBuilder outside the context of processing a request or to apply a custom baseUrl not matching the current request.- Parameters:
builder- the builder for the base URL; the builder will be cloned and therefore not modified and may be re-used for further calls.controllerType- the controller to build a URI for- Returns:
- a UriComponentsBuilder instance (never
null)
-
fromMethodName
public static org.springframework.web.util.UriComponentsBuilder fromMethodName(Class<?> controllerType, String methodName, Object... args) Create aUriComponentsBuilderfrom the mapping of a controller method and an array of method argument values. This method delegates tofromMethod(Class, Method, Object...).- Parameters:
controllerType- the controllermethodName- the method nameargs- the argument values- Returns:
- a UriComponentsBuilder instance, never
null - Throws:
IllegalArgumentException- if there is no matching or if there is more than one matching method
-
fromMethodName
public static org.springframework.web.util.UriComponentsBuilder fromMethodName(org.springframework.web.util.UriComponentsBuilder builder, Class<?> controllerType, String methodName, Object... args) An alternative tofromMethodName(Class, String, Object...)that accepts aUriComponentsBuilderrepresenting the base URL. This is useful when using MvcUriComponentsBuilder outside the context of processing a request or to apply a custom baseUrl not matching the current request.- Parameters:
builder- the builder for the base URL; the builder will be cloned and therefore not modified and may be re-used for further calls.controllerType- the controllermethodName- the method nameargs- the argument values- Returns:
- a UriComponentsBuilder instance, never
null - Throws:
IllegalArgumentException- if there is no matching or if there is more than one matching method
-
fromMethodCall
Create aUriComponentsBuilderby invoking a "mock" controller method. The controller method and the supplied argument values are then used to delegate tofromMethod(Class, Method, Object...).For example, given this controller:
@RequestMapping("/people/{id}/addresses") class AddressController { @RequestMapping("/{country}") public HttpEntityA UriComponentsBuilder can be created:getAddressesForCountry(@PathVariable String country) { ... } @RequestMapping(value="/", method=RequestMethod.POST) public void addAddress(Address address) { ... } } // Inline style with static import of "MvcUriComponentsBuilder.on" MvcUriComponentsBuilder.fromMethodCall( on(AddressController.class).getAddressesForCountry("US")).buildAndExpand(1); // Longer form useful for repeated invocation (and void controller methods) AddressController controller = MvcUriComponentsBuilder.on(AddressController.class); controller.addAddress(null); builder = MvcUriComponentsBuilder.fromMethodCall(controller); controller.getAddressesForCountry("US") builder = MvcUriComponentsBuilder.fromMethodCall(controller);- Parameters:
info- either the value returned from a "mock" controller invocation or the "mock" controller itself after an invocation- Returns:
- a UriComponents instance
-
fromMethodCall
public static org.springframework.web.util.UriComponentsBuilder fromMethodCall(org.springframework.web.util.UriComponentsBuilder builder, Object info) An alternative tofromMethodCall(Object)that accepts aUriComponentsBuilderrepresenting the base URL. This is useful when using MvcUriComponentsBuilder outside the context of processing a request or to apply a custom baseUrl not matching the current request.- Parameters:
builder- the builder for the base URL; the builder will be cloned and therefore not modified and may be re-used for further calls.info- either the value returned from a "mock" controller invocation or the "mock" controller itself after an invocation- Returns:
- a UriComponents instance
-
fromMappingName
public static FrameworkMvcUriComponentsBuilder.MethodArgumentBuilder fromMappingName(String mappingName) Create a URL from the name of a Spring MVC controller method's request mapping.The configured
HandlerMethodMappingNamingStrategydetermines the names of controller method request mappings at startup. By default all mappings are assigned a name based on the capital letters of the class name, followed by "#" as separator, and then the method name. For example "PC#getPerson" for a class named PersonController with method getPerson. In case the naming convention does not produce unique results, an explicit name may be assigned through the name attribute of the@RequestMappingannotation.This is aimed primarily for use in view rendering technologies and EL expressions. The Spring URL tag library registers this method as a function called "mvcUrl".
For example, given this controller:
@RequestMapping("/people") class PersonController { @RequestMapping("/{id}") public HttpEntitygetPerson(@PathVariable String id) { ... } } A JSP can prepare a URL to the controller method as follows:
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %> <a href="${s:mvcUrl('PC#getPerson').arg(0,"123").build()}">Get Person</a>Note that it's not necessary to specify all arguments. Only the ones required to prepare the URL, mainly
@RequestParamand@PathVariable).- Parameters:
mappingName- the mapping name- Returns:
- a builder to prepare the URI String
- Throws:
IllegalArgumentException- if the mapping name is not found or if there is no unique match- Since:
- 4.1
-
fromMappingName
public static FrameworkMvcUriComponentsBuilder.MethodArgumentBuilder fromMappingName(org.springframework.web.util.UriComponentsBuilder builder, String name) An alternative tofromMappingName(String)that accepts aUriComponentsBuilderrepresenting the base URL. This is useful when using MvcUriComponentsBuilder outside the context of processing a request or to apply a custom baseUrl not matching the current request.- Parameters:
builder- the builder for the base URL; the builder will be cloned and therefore not modified and may be re-used for further calls.name- the mapping name- Returns:
- a builder to prepare the URI String
- Throws:
IllegalArgumentException- if the mapping name is not found or if there is no unique match- Since:
- 4.2
-
fromMethod
public static org.springframework.web.util.UriComponentsBuilder fromMethod(Class<?> controllerType, Method method, Object... args) Create aUriComponentsBuilderfrom the mapping of a controller method and an array of method argument values. The array of values must match the signature of the controller method. Values for@RequestParamand@PathVariableare used for building the URI (via implementations ofUriComponentsContributor) while remaining argument values are ignored and can benull.- Parameters:
controllerType- the controller typemethod- the controller methodargs- argument values for the controller method- Returns:
- a UriComponentsBuilder instance, never
null - Since:
- 4.2
-
fromMethod
public static org.springframework.web.util.UriComponentsBuilder fromMethod(org.springframework.web.util.UriComponentsBuilder baseUrl, Class<?> controllerType, Method method, Object... args) An alternative tofromMethod(Class, Method, Object...)that accepts aUriComponentsBuilderrepresenting the base URL. This is useful when using MvcUriComponentsBuilder outside the context of processing a request or to apply a custom baseUrl not matching the current request.- Parameters:
baseUrl- the builder for the base URL; the builder will be cloned and therefore not modified and may be re-used for further calls.controllerType- the controller typemethod- the controller methodargs- argument values for the controller method- Returns:
- a UriComponentsBuilder instance (never
null) - Since:
- 4.2
-
fromMethod
@Deprecated public static org.springframework.web.util.UriComponentsBuilder fromMethod(Method method, Object... args) Deprecated.as of 4.2, this is deprecated in favor of the overloaded method that also accepts a controllerType argument -
on
Return a "mock" controller instance. When an@RequestMappingmethod on the controller is invoked, the supplied argument values are remembered and the result can then be used to create aUriComponentsBuilderviafromMethodCall(Object).Note that this is a shorthand version of
controller(Class)intended for inline use (with a static import), for example:MvcUriComponentsBuilder.fromMethodCall(on(FooController.class).getFoo(1)).build();
- Parameters:
controllerType- the target controller
-
controller
Return a "mock" controller instance. When an@RequestMappingmethod on the controller is invoked, the supplied argument values are remembered and the result can then be used to createUriComponentsBuilderviafromMethodCall(Object).This is a longer version of
on(Class). It is needed with controller methods returning void as well for repeated invocations.FooController fooController = controller(FooController.class); fooController.saveFoo(1, null); builder = MvcUriComponentsBuilder.fromMethodCall(fooController); fooController.saveFoo(2, null); builder = MvcUriComponentsBuilder.fromMethodCall(fooController);
- Parameters:
controllerType- the target controller
-
withController
An alternative tofromController(Class)for use with an instance of this class created via a call torelativeTo(org.springframework.web.util.UriComponentsBuilder).- Since:
- 4.2
-
withMethodName
public org.springframework.web.util.UriComponentsBuilder withMethodName(Class<?> controllerType, String methodName, Object... args) An alternative tofromMethodName(Class, String, Object...)} for use with an instance of this class created viarelativeTo(org.springframework.web.util.UriComponentsBuilder).- Since:
- 4.2
-
withMethodCall
An alternative tofromMethodCall(Object)for use with an instance of this class created viarelativeTo(org.springframework.web.util.UriComponentsBuilder).- Since:
- 4.2
-
withMappingName
An alternative tofromMappingName(String)for use with an instance of this class created viarelativeTo(org.springframework.web.util.UriComponentsBuilder).- Since:
- 4.2
-
withMethod
public org.springframework.web.util.UriComponentsBuilder withMethod(Class<?> controllerType, Method method, Object... args) An alternative tofromMethod(Class, Method, Object...)for use with an instance of this class created viarelativeTo(org.springframework.web.util.UriComponentsBuilder).- Since:
- 4.2
-