spring-projects / spring-hateoas

Spring HATEOAS - Library to support implementing representations for hyper-text driven REST web services.
https://spring.io/projects/spring-hateoas
Apache License 2.0
1.03k stars 476 forks source link

Traverson issues navigating relative paths #740

Open rpalcolea opened 5 years ago

rpalcolea commented 5 years ago

Hi,

I'm trying to do a follow with Traveson for a json like this one:

{
    "_embedded": {
        "repositories": [{
            "cloneUrl": "ssh://git@github.com/org/repo.git",
            "_links": {
                "applications": {
                    "href": "/api/applications/repository/1"
                }
            }
        }]
    },
    "page": {
        "size": 100,
        "totalElements": 1,
        "totalPages": 1,
        "number": 1
    }
}

The code looks like:

 val traverson = Traverson(URI(myUrl), MediaTypes.HAL_JSON)
        val tb = traverson.follow("$._embedded.repositories[0]._links.applications.href")
        val typeReferences = typeReference<Resources<MyClass>>()
        val resources = tb.toObject(typeReferences)
        val repos = resources.content

However, this results in java.lang.IllegalArgumentException: URI is not absolute.

Is there a way to get Traverson working with the original base URI? I can't find a method on the API to accomplish this

gregturn commented 5 years ago

It would help if you posted a full stack trace.

When Spring HATEOAS attempts to navigate over a URI, it does expand any variables and then turns it into a URL, which if you don't specify the whole thing, can fail.

We might need some defensive handling of path-only URIs.

gregturn commented 5 years ago

RestTemplate needs a full URI to operate properly.

If we detect a relative path it makes sense to try and merge with the base URI.

AntonioDell commented 4 years ago

Is there any update on the issue or maybe a workaround to handle relative paths?