Spring HATEOAS 1.0, BaseUriLinkBuilder removed

Viewed 373

I updated my Spring Boot project to version 2.2. I was using HATEAOS so I had to convert serveral part of the application as stated here https://spring.io/blog/2019/03/05/spring-hateoas-1-0-m1-released

I have only a problem with BaseUriLinkBuilder. I had this class:

/**
 * Utility class that fix the URI created adding the base path.
 * {@linkplain "https://github.com/spring-projects/spring-hateoas/issues/434}"
 *
 * @author Daniele Renda
 */
@Service
@Log4j2
public class BasePathAwareLinks {

    private final URI contextBaseURI;
    private final URI restBaseURI;

    @Autowired
    public BasePathAwareLinks(ServletContext servletContext, RepositoryRestConfiguration config) {
        contextBaseURI = URI.create(servletContext.getContextPath());
        restBaseURI = config.getBasePath();
    }

    public WebMvcLinkBuilder underBasePath(WebMvcLinkBuilder linkBuilder) {
        try {
            URI uri = linkBuilder.toUri();
            URI origin = new URI(uri.getScheme(), uri.getAuthority(), null, null, null);
            URI suffix = new URI(null, null, uri.getPath(), uri.getQuery(), uri.getFragment());
            return BaseUriLinkBuilder.create(origin).slash(contextBaseURI).slash(restBaseURI).slash(suffix);
        } catch (URISyntaxException e) {
            log.error("", e);
        }
        return null;
    }
}

But it seems that BaseUriLinkBuilder doesn't exist anymore. I didn't find any reference to this change. Any hint how to fix the problem?

1 Answers

I fixed it like this:

    import org.springframework.hateoas.server.mvc.BasicLinkBuilder;

    public LinkBuilder underBasePath(WebMvcLinkBuilder linkBuilder) {
    return BasicLinkBuilder.linkToCurrentMapping()
            .slash(restBaseURI)
            .slash(contextBaseURI.relativize(URI.create(linkBuilder.toUri().getPath())));
}
Related