#372 - Added support for Map parameters to LinkBuilderFactory.linkTo(…).
This commit is contained in:
committed by
Oliver Gierke
parent
a5dd1d54b1
commit
23a9017214
@@ -15,6 +15,8 @@
|
||||
*/
|
||||
package org.springframework.hateoas;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Factory for {@link LinkBuilder} instances.
|
||||
*
|
||||
@@ -41,4 +43,15 @@ public interface LinkBuilderFactory<T extends LinkBuilder> {
|
||||
* @return
|
||||
*/
|
||||
T linkTo(Class<?> target, Object... parameters);
|
||||
|
||||
/**
|
||||
* Creates a new {@link LinkBuilder} with a base of the mapping annotated to the given target class (controller,
|
||||
* service, etc.). Parameter map is used to fill up potentially available path variables in the class scope request
|
||||
* mapping.
|
||||
*
|
||||
* @param target must not be {@literal null}.
|
||||
* @param parameters must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
T linkTo(Class<?> target, Map<String, ?> parameters);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||
import org.springframework.web.util.UriComponents;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* {@link LinkBuilder} to derive URI mappings from a JAX-RS {@link Path} annotation.
|
||||
*
|
||||
@@ -72,6 +74,24 @@ public class JaxRsLinkBuilder extends LinkBuilderSupport<JaxRsLinkBuilder> {
|
||||
return builder.slash(expandedComponents);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link JaxRsLinkBuilder} instance to link to the {@link Path} mapping tied to the given class binding
|
||||
* the given parameters to the URI template.
|
||||
*
|
||||
* @param service the class to discover the annotation on, must not be {@literal null}.
|
||||
* @param parameters map of additional parameters to bind to the URI template declared in the annotation, must not be
|
||||
* {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static JaxRsLinkBuilder linkTo(Class<?> service, Map<String, ?> parameters) {
|
||||
|
||||
JaxRsLinkBuilder builder = new JaxRsLinkBuilder(ServletUriComponentsBuilder.fromCurrentServletMapping());
|
||||
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(DISCOVERER.getMapping(service)).build();
|
||||
UriComponents expandedComponents = uriComponents.expand(parameters);
|
||||
return builder.slash(expandedComponents);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.UriComponentsLinkBuilder#getThis()
|
||||
|
||||
@@ -18,6 +18,8 @@ package org.springframework.hateoas.jaxrs;
|
||||
import org.springframework.hateoas.LinkBuilder;
|
||||
import org.springframework.hateoas.LinkBuilderFactory;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Factory for {@link LinkBuilder} instances based on the path mapping annotated on the given JAX-RS service.
|
||||
*
|
||||
@@ -42,4 +44,13 @@ public class JaxRsLinkBuilderFactory implements LinkBuilderFactory<JaxRsLinkBuil
|
||||
public JaxRsLinkBuilder linkTo(Class<?> service, Object... parameters) {
|
||||
return JaxRsLinkBuilder.linkTo(service, parameters);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.LinkBuilderFactory#linkTo(java.lang.Class, java.util.Map)
|
||||
*/
|
||||
@Override
|
||||
public JaxRsLinkBuilder linkTo(Class<?> service, Map<String, ?> parameters) {
|
||||
return JaxRsLinkBuilder.linkTo(service, parameters);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import static org.springframework.util.StringUtils.*;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
@@ -101,11 +102,34 @@ public class ControllerLinkBuilder extends LinkBuilderSupport<ControllerLinkBuil
|
||||
return builder.slash(expandedComponents);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link ControllerLinkBuilder} with a base of the mapping annotated to the given controller class.
|
||||
* Parameter map is used to fill up potentially available path variables in the class scope request
|
||||
* mapping.
|
||||
*
|
||||
* @param controller the class to discover the annotation on, must not be {@literal null}.
|
||||
* @param parameters additional parameters to bind to the URI template declared in the annotation, must not be
|
||||
* {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public static ControllerLinkBuilder linkTo(Class<?> controller, Map<String, ?> parameters) {
|
||||
|
||||
Assert.notNull(controller);
|
||||
|
||||
ControllerLinkBuilder builder = new ControllerLinkBuilder(getBuilder());
|
||||
String mapping = DISCOVERER.getMapping(controller);
|
||||
|
||||
UriComponents uriComponents = UriComponentsBuilder.fromUriString(mapping == null ? "/" : mapping).build();
|
||||
UriComponents expandedComponents = uriComponents.expand(parameters);
|
||||
|
||||
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, parameters);
|
||||
return linkTo(method.getDeclaringClass(), method, parameters);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -95,6 +95,15 @@ public class ControllerLinkBuilderFactory implements MethodLinkBuilderFactory<Co
|
||||
return ControllerLinkBuilder.linkTo(controller, parameters);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.LinkBuilderFactory#linkTo(java.lang.Class, java.util.Map)
|
||||
*/
|
||||
@Override
|
||||
public ControllerLinkBuilder linkTo(Class<?> controller, Map<String, ?> parameters) {
|
||||
return ControllerLinkBuilder.linkTo(controller, parameters);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.hateoas.MethodLinkBuilderFactory#linkTo(java.lang.Class, java.lang.reflect.Method, java.lang.Object[])
|
||||
|
||||
Reference in New Issue
Block a user