Incorporate feedback from @rstoyanchev.
Improve ResourceAssemblerSupport by introducing a dedicated method instead of an overload. Got rid of EntityId in the course. ControllerLinkBuilder now inspects type hierarchy for @RequestMapping annotations instead of the class only. Made links list in ResourceSupport final. Polished variable names and JavaDoc.
This commit is contained in:
@@ -31,7 +31,7 @@ import org.springframework.util.Assert;
|
||||
public class ResourceSupport implements Identifiable<Link> {
|
||||
|
||||
@XmlElement(name = "link", namespace = Link.ATOM_NAMESPACE)
|
||||
private List<Link> links;
|
||||
private final List<Link> links;
|
||||
|
||||
public ResourceSupport() {
|
||||
this.links = new ArrayList<Link>();
|
||||
|
||||
@@ -35,16 +35,16 @@ import org.springframework.web.util.UriTemplate;
|
||||
*/
|
||||
public class ControllerLinkBuilder {
|
||||
|
||||
private final UriComponents builder;
|
||||
private final UriComponents uriComponents;
|
||||
|
||||
/**
|
||||
* Creates a new {@link ControllerLinkBuilder}.
|
||||
*
|
||||
* @param builder must not be {@literal null}.
|
||||
* @param uriComponents must not be {@literal null}.
|
||||
*/
|
||||
private ControllerLinkBuilder(UriComponentsBuilder builder) {
|
||||
Assert.notNull(builder);
|
||||
this.builder = builder.build();
|
||||
this.uriComponents = builder.build();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -59,7 +59,7 @@ public class ControllerLinkBuilder {
|
||||
|
||||
public static ControllerLinkBuilder linkTo(Class<?> controller, Object... parameters) {
|
||||
|
||||
RequestMapping annotation = controller.getAnnotation(RequestMapping.class);
|
||||
RequestMapping annotation = AnnotationUtils.findAnnotation(controller, RequestMapping.class);
|
||||
String[] mapping = annotation == null ? new String[0] : (String[]) AnnotationUtils.getValue(annotation);
|
||||
|
||||
if (mapping.length > 1) {
|
||||
@@ -89,12 +89,12 @@ public class ControllerLinkBuilder {
|
||||
}
|
||||
|
||||
String[] segments = StringUtils.tokenizeToStringArray(object.toString(), "/");
|
||||
return new ControllerLinkBuilder(UriComponentsBuilder.fromUri(builder.toUri()).pathSegment(segments));
|
||||
return new ControllerLinkBuilder(UriComponentsBuilder.fromUri(uriComponents.toUri()).pathSegment(segments));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the given {@link AbstractEntity}'s id as sub-resource. Will simply return the current builder if the given
|
||||
* entity is {@literal null}.
|
||||
* Adds the given {@link AbstractEntity}'s id as sub-resource. Will simply return the current uriComponents if the
|
||||
* given entity is {@literal null}.
|
||||
*
|
||||
* @param identifyable
|
||||
* @return
|
||||
@@ -109,12 +109,12 @@ public class ControllerLinkBuilder {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a URI resulting from the builder.
|
||||
* Returns a URI resulting from the uriComponents.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public URI toUri() {
|
||||
return builder.encode().toUri();
|
||||
return uriComponents.encode().toUri();
|
||||
}
|
||||
|
||||
public Link withRel(String rel) {
|
||||
|
||||
@@ -36,25 +36,6 @@ import org.springframework.util.Assert;
|
||||
public abstract class ResourceAssemblerSupport<T extends Identifiable<?>, D extends ResourceSupport> implements
|
||||
ResourceAssembler<T, D> {
|
||||
|
||||
public static class EntityId {
|
||||
|
||||
private final Object id;
|
||||
|
||||
public EntityId(Object id) {
|
||||
Assert.notNull(id);
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public static EntityId id(Object object) {
|
||||
return new EntityId(object);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.id.toString();
|
||||
}
|
||||
}
|
||||
|
||||
private final Class<?> controllerClass;
|
||||
private final Class<D> resourceType;
|
||||
|
||||
@@ -77,11 +58,12 @@ public abstract class ResourceAssemblerSupport<T extends Identifiable<?>, D exte
|
||||
* Converts all given entities into resources.
|
||||
*
|
||||
* @see #toResource(Object)
|
||||
* @param entities
|
||||
* @param entities must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
public List<D> toResources(Iterable<? extends T> entities) {
|
||||
|
||||
Assert.notNull(entities);
|
||||
List<D> result = new ArrayList<D>();
|
||||
|
||||
for (T entity : entities) {
|
||||
@@ -102,21 +84,21 @@ public abstract class ResourceAssemblerSupport<T extends Identifiable<?>, D exte
|
||||
}
|
||||
|
||||
protected D createResource(T entity, Object... parameters) {
|
||||
return createResource(entity, EntityId.id(entity.getId()), parameters);
|
||||
return createResourceWithId(entity.getId(), entity, parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new resource with a self link to the given id.
|
||||
*
|
||||
* @param entity
|
||||
* @param id
|
||||
* @param entity must not be {@literal null}.
|
||||
* @param id must not be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
protected D createResource(T entity, EntityId id) {
|
||||
return createResource(entity, id, new Object[0]);
|
||||
protected D createResourceWithId(Object id, T entity) {
|
||||
return createResourceWithId(id, entity, new Object[0]);
|
||||
}
|
||||
|
||||
protected D createResource(T entity, EntityId id, Object... parameters) {
|
||||
protected D createResourceWithId(Object id, T entity, Object... parameters) {
|
||||
|
||||
Assert.notNull(entity);
|
||||
Assert.notNull(id);
|
||||
@@ -154,4 +136,4 @@ public abstract class ResourceAssemblerSupport<T extends Identifiable<?>, D exte
|
||||
protected D instantiateResource(T entity) {
|
||||
return BeanUtils.instantiateClass(resourceType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user