This commit is contained in:
Phillip Webb
2013-10-25 11:43:13 -07:00
parent 8bd480441e
commit 12e896ed8b
7 changed files with 30 additions and 50 deletions

View File

@@ -63,10 +63,9 @@ public class DefaultMvcUrls implements MvcUrls {
* {@link HandlerMethodArgumentResolver}s. Since both of these tend to be implemented
* by the same class, the most convenient option is to obtain the configured
* {@code HandlerMethodArgumentResolvers} in the {@code RequestMappingHandlerAdapter}
* and provide that to this contstructor.
*
* and provide that to this constructor.
* @param uriComponentsContributors a collection of {@link UriComponentsContributor}
* or {@link HandlerMethodArgumentResolver}s.
* or {@link HandlerMethodArgumentResolver}s.
*/
public DefaultMvcUrls(Collection<?> uriComponentsContributors) {
this(uriComponentsContributors, null);
@@ -77,26 +76,22 @@ public class DefaultMvcUrls implements MvcUrls {
* {@link HandlerMethodArgumentResolver}s. Since both of these tend to be implemented
* by the same class, the most convenient option is to obtain the configured
* {@code HandlerMethodArgumentResolvers} in the {@code RequestMappingHandlerAdapter}
* and provide that to this contstructor.
* <p>
* If the {@link ConversionService} argument is {@code null},
* {@link DefaultFormattingConversionService} will be used by default.
* and provide that to this constructor.
*
* <p>If the {@link ConversionService} argument is {@code null},
* {@link DefaultFormattingConversionService} will be used by default.
* @param uriComponentsContributors a collection of {@link UriComponentsContributor}
* or {@link HandlerMethodArgumentResolver}s.
* or {@link HandlerMethodArgumentResolver}s.
* @param conversionService a ConversionService to use when method argument values
* need to be formatted as Strings before being added to the URI
* need to be formatted as Strings before being added to the URI
*/
public DefaultMvcUrls(Collection<?> uriComponentsContributors, ConversionService conversionService) {
Assert.notNull(uriComponentsContributors, "'uriComponentsContributors' must not be null");
for (Object contributor : uriComponentsContributors) {
if (contributor instanceof UriComponentsContributor) {
this.contributors.add((UriComponentsContributor) contributor);
}
}
this.conversionService = (conversionService != null) ?
conversionService : new DefaultFormattingConversionService();
}
@@ -118,14 +113,12 @@ public class DefaultMvcUrls implements MvcUrls {
private UriComponents applyContributers(UriComponentsBuilder builder, Method method,
Object[] argumentValues, Map<String, Object> uriVars) {
if (this.contributors.isEmpty()) {
return builder.buildAndExpand(uriVars);
}
int paramCount = method.getParameters().length;
int argCount = argumentValues.length;
Assert.isTrue(paramCount == argCount, "Number of method parameters " + paramCount +
" does not match number of argument values " + argCount);
@@ -145,7 +138,6 @@ public class DefaultMvcUrls implements MvcUrls {
@Override
public UriComponents linkToMethodOn(Object mockController) {
Assert.isInstanceOf(ControllerMethodValues.class, mockController);
ControllerMethodValues controllerMethodValues = (ControllerMethodValues) mockController;
@@ -162,7 +154,6 @@ public class DefaultMvcUrls implements MvcUrls {
}
private void addTypeLevelUriVaris(ControllerMethodValues info, Map<String, Object> uriVariables) {
Object[] values = info.getTypeLevelUriVariables();
if (!ObjectUtils.isEmpty(values)) {

View File

@@ -43,7 +43,6 @@ import org.springframework.web.util.UriComponents;
*
* @author Oliver Gierke
* @author Rossen Stoyanchev
*
* @since 4.0
*/
public class MvcUrlUtils {
@@ -67,7 +66,6 @@ public class MvcUrlUtils {
logger.warn("Multiple class level mappings on " + controllerType.getName() + ", using the first one");
}
return annot.value()[0];
}
/**
@@ -97,7 +95,6 @@ public class MvcUrlUtils {
* invoked method and argument values are remembered, and a "mock" value is returned
* so it can be used to help prepare a {@link UriComponents} through
* {@link MvcUrls#linkToMethodOn(Object)}.
*
* @param controllerType the type of controller to mock, must not be {@literal null}.
* @param typeLevelUriVariables URI variables to expand into the type-level mapping
* @return the created controller instance
@@ -109,7 +106,6 @@ public class MvcUrlUtils {
@SuppressWarnings("unchecked")
private static <T> T initProxy(Class<?> type, ControllerMethodInvocationInterceptor interceptor) {
if (type.isInterface()) {
ProxyFactory factory = new ProxyFactory(EmptyTargetSource.INSTANCE);
factory.addInterface(type);
@@ -192,7 +188,7 @@ public class MvcUrlUtils {
* <p>
* Instances of this interface are returned from
* {@link MvcUrlUtils#controller(Class, Object...) controller(Class, Object...)} and
* are needed for {@link MvcUrls#linkToMethodOn(ControllerMethodValues)}.
* are needed for {@link MvcUrls#linkToMethodOn(Object)}.
*/
public interface ControllerMethodValues {

View File

@@ -27,19 +27,18 @@ import org.springframework.web.util.UriComponentsBuilder;
/**
* A contract for creating URLs by referencing Spring MVC controllers and methods.
* <p>
* The MVC Java config and the MVC namespace automatically create an instance of this
*
* <p>The MVC Java config and the MVC namespace automatically create an instance of this
* contract for use in controllers and anywhere else during the processing of a request.
* The best way for access it is to have it autowired, or otherwise injected either by
* type or also qualified by name ("mvcUrls") if necessary.
* <p>
* If not using either option, with explicit configuration it's easy to create an instance
* of {@link DefaultMvcUrls} in Java config or in XML configuration, use
*
* <p>If not using either option, with explicit configuration it's easy to create an
* instance of {@link DefaultMvcUrls} in Java config or in XML configuration, use
* {@link DefaultMvcUrlsFactoryBean}.
*
* @author Oliver Gierke
* @author Rossen Stoyanchev
*
* @since 4.0
*/
public interface MvcUrls {
@@ -50,9 +49,7 @@ public interface MvcUrls {
* the Servlet mapping as well as the portion of the path matching to the controller
* level request mapping. If the controller contains multiple mappings, the
* {@link DefaultMvcUrls} will use the first one.
*
* @param controllerType the controller type to create a URL to
*
* @return a builder that can be used to further build the {@link UriComponents}.
*/
UriComponentsBuilder linkToController(Class<?> controllerType);
@@ -60,19 +57,15 @@ public interface MvcUrls {
/**
* Create a {@link UriComponents} by pointing to a controller method along with method
* argument values.
* <p>
* Type and method-level mappings of the controller method are extracted and the
*
* <p>Type and method-level mappings of the controller method are extracted and the
* resulting {@link UriComponents} is further enriched with method argument values from
* {@link PathVariable} and {@link RequestParam} parameters. Any other arguments not
* relevant to the building of the URL can be provided as {@literal null} and will be
* ignored. Support for additional custom arguments can be added through a
* {@link UriComponentsContributor}.
*
* FIXME Type-level URI template variables?
*
* @param method the target controller method
* @param argumentValues argument values matching to method parameters
*
* @return UriComponents instance, never {@literal null}
*/
UriComponents linkToMethod(Method method, Object... argumentValues);
@@ -88,10 +81,10 @@ public interface MvcUrls {
* class AddressController {
*
* &#064;RequestMapping("/{country}")
* public HttpEntity<Void> getAddressesForCountry(&#064;PathVariable String country) { }
* public HttpEntity&lt;Void&gt; getAddressesForCountry(&#064;PathVariable String country) { ... }
*
* &#064;RequestMapping(value="/", method=RequestMethod.POST)
* public void addAddress(Address address) { }
* public void addAddress(Address address) { ... }
* }
*
* // short-hand style with static import of MvcUrlUtils.controller
@@ -104,16 +97,13 @@ public interface MvcUrls {
* controller.addAddress(null);
*
* mvcUrls.linkToMethodOn(controller);
*
* </pre>
*
* The above mechanism supports {@link PathVariable} and {@link RequestParam} method
* arguments. Any other arguments can be provided as {@literal null} and will be
* ignored. Additional custom arguments can be added through an implementation of
* {@link UriComponentsContributor}.
*
* @param mockController created via {@link MvcUrlUtils#controller(Class, Object...)}
*
* @return UriComponents instance, never {@literal null}
*/
UriComponents linkToMethodOn(Object mockController);