#192 - Added linkTo(Class<?>, Method, Object...) to MethodLinkBuilderFactory.

The newly introduced method is needed to make sure the correct mappings are looked up if the method given is actually not declared on the class the method shall eventually be invoked on.

This is basically allowing to try to build links, similar to the dummy invocation mechanism but effectively avoiding the overhead of creating such invocation manually (as it would usually require reflection to be used).
This commit is contained in:
Oliver Gierke
2014-06-05 16:19:10 +02:00
parent daeae2e98a
commit 673f82d388
4 changed files with 51 additions and 4 deletions

View File

@@ -88,10 +88,24 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
return builder.slash(expandedComponents);
}
/*
* @see org.springframework.hateoas.MethodLinkBuilderFactory#linkTo(Method, Object...)
*/
public static ControllerLinkBuilder linkTo(Method method, Object... parameters) {
return linkTo(method.getDeclaringClass(), method);
}
UriTemplate template = new UriTemplate(DISCOVERER.getMapping(method));
/*
* @see org.springframework.hateoas.MethodLinkBuilderFactory#linkTo(Class<?>, Method, Object...)
*/
public static ControllerLinkBuilder linkTo(Class<?> controller, Method method, Object... parameters) {
Assert.notNull(controller, "Controller type must not be null!");
Assert.notNull(method, "Method must not be null!");
UriTemplate template = new UriTemplate(DISCOVERER.getMapping(controller, method));
URI uri = template.expand(parameters);
return new ControllerLinkBuilder(getBuilder()).slash(uri);
}

View File

@@ -90,6 +90,15 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
return ControllerLinkBuilder.linkTo(controller, parameters);
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.MethodLinkBuilderFactory#linkTo(java.lang.Class, java.lang.reflect.Method, java.lang.Object[])
*/
@Override
public ControllerLinkBuilder linkTo(Class<?> controller, Method method, Object... parameters) {
return ControllerLinkBuilder.linkTo(controller, method, parameters);
}
/*
* (non-Javadoc)
* @see org.springframework.hateoas.MethodLinkBuilderFactory#linkTo(java.lang.Object)