DATAREST-1463 - Adapt to API changes in LinkBuilderSupport.

This commit is contained in:
Oliver Drotbohm
2019-12-09 10:21:13 +01:00
parent 406a3a1ff1
commit 2b8e7afbf3
2 changed files with 25 additions and 13 deletions

View File

@@ -23,10 +23,12 @@ import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.data.rest.core.Path;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UrlPathHelper;
@@ -152,10 +154,21 @@ public class BaseUri {
*/
public UriComponentsBuilder getUriComponentsBuilder() {
if (baseUri.isAbsolute()) {
return UriComponentsBuilder.fromUri(baseUri);
}
return baseUri.isAbsolute() //
? UriComponentsBuilder.fromUri(baseUri) //
: ServletUriComponentsBuilder.fromCurrentServletMapping().path(baseUri.toString());
}
return ServletUriComponentsBuilder.fromCurrentServletMapping().path(baseUri.toString());
/**
* Returns the {@link UriComponents} for the given {@link Path} appended to the current {@link BaseUri}.
*
* @param path must not be {@literal null}.
* @return
*/
public UriComponents appendPath(Path path) {
Assert.notNull(path, "Path must not be null!");
return getUriComponentsBuilder().path(path.toString()).build();
}
}

View File

@@ -27,7 +27,7 @@ import org.springframework.hateoas.Link;
import org.springframework.hateoas.server.LinkBuilder;
import org.springframework.hateoas.server.core.LinkBuilderSupport;
import org.springframework.util.Assert;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriComponents;
/**
* {@link LinkBuilder} to be able to create links pointing to repositories.
@@ -45,19 +45,18 @@ public class RepositoryLinkBuilder extends LinkBuilderSupport<RepositoryLinkBuil
* @param baseUri
*/
public RepositoryLinkBuilder(ResourceMetadata metadata, BaseUri baseUri) {
this(metadata, baseUri.getUriComponentsBuilder().path(metadata.getPath().toString()), Collections.emptyList());
this(metadata, baseUri.appendPath(metadata.getPath()), Collections.emptyList());
}
/**
* Creates a new {@link RepositoryLinkBuilder} with the given {@link ResourceMetadata} and
* {@link UriComponentsBuilder}.
* Creates a new {@link RepositoryLinkBuilder} with the given {@link ResourceMetadata} and {@link UriComponents}.
*
* @param metadata must not be {@literal null}.
* @param builder must not be {@literal null}.
* @param components must not be {@literal null}.
*/
private RepositoryLinkBuilder(ResourceMetadata metadata, UriComponentsBuilder builder, List<Affordance> affordances) {
private RepositoryLinkBuilder(ResourceMetadata metadata, UriComponents components, List<Affordance> affordances) {
super(builder, affordances);
super(components, affordances);
Assert.notNull(metadata, "ResourceMetadata must not be null!");
@@ -89,8 +88,8 @@ public class RepositoryLinkBuilder extends LinkBuilderSupport<RepositoryLinkBuil
* @see org.springframework.hateoas.server.core.LinkBuilderSupport#createNewInstance(org.springframework.web.util.UriComponentsBuilder, java.util.List)
*/
@Override
protected RepositoryLinkBuilder createNewInstance(UriComponentsBuilder builder, List<Affordance> affordances) {
return new RepositoryLinkBuilder(this.metadata, builder, affordances);
protected RepositoryLinkBuilder createNewInstance(UriComponents components, List<Affordance> affordances) {
return new RepositoryLinkBuilder(this.metadata, components, affordances);
}
/*