Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/org/springframework/hateoas/server/LinkBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public interface LinkBuilder {
*/
LinkBuilder slash(@Nullable Object object);

/**
* Adds {@link String} representation of the given object as identifier of an item resource
* from the current collection resource.
*
* This method is an optimized version of {@link #slash(Object)} mean to be used only for identifiers.
*
* @param id can be {@literal null}.
*/
LinkBuilder slashId(@Nullable Object id);

/**
* Creates a URI of the link built by the current builder instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public abstract class AbstractEntityLinks implements EntityLinks {
*/
@Override
public LinkBuilder linkForItemResource(Class<?> type, Object id) {
return linkFor(type).slash(id);
return linkFor(type).slashId(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public Link linkToCollectionResource(Class<?> entity) {
*/
@Override
public Link linkToItemResource(Class<?> entity, Object id) {
return linkFor(entity).slash(id).withSelfRel();
return linkFor(entity).slashId(id).withSelfRel();
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ protected LinkBuilderSupport(UriComponents components, List<Affordance> affordan
*/
public T slash(@Nullable Object object) {

object = object instanceof Optional ? ((Optional<?>) object).orElse(null) : object;
object = unwrapPotentialOptional(object);

if (object == null) {
return getThis();
Expand All @@ -97,6 +97,25 @@ public T slash(@Nullable Object object) {
return slash(UriComponentsBuilder.fromUriString(path).build(), false);
}

public T slashId(@Nullable Object id) {

id = unwrapPotentialOptional(id);

if (id == null) {
return getThis();
}

UriComponentsBuilder builder = UriComponentsBuilder.newInstance().uriComponents(this.components);
builder.pathSegment(encodePath(id));

return createNewInstance(builder.build(), affordances);
}

@Nullable
private Object unwrapPotentialOptional(@Nullable Object id) {
return id instanceof Optional ? ((Optional<?>) id).orElse(null) : id;
}

protected T slash(UriComponents components, boolean encoded) {

UriComponentsBuilder builder = UriComponentsBuilder.newInstance().uriComponents(this.components);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ protected D createModelWithId(Object id, T entity, Object... parameters) {
Assert.notNull(id, "Id must not be null!");

D instance = instantiateModel(entity);
instance.add(linkTo(this.controllerClass, parameters).slash(id).withSelfRel());
instance.add(linkTo(this.controllerClass, parameters).slashId(id).withSelfRel());
return instance;
}

Expand Down