I have a REST endpoint (like http://myapi.rest.com/someEndpoint?offset=40&limit=20) that vends out HAL JSON response like the following
{
"_embedded": {
"listOfObjects": [
{
"att1":"value1",
"att2":"value2"
}
]
},
"_links": {
"self": {
"href": "http://myapi.rest.com/someEndpoint?offset=40&limit=20"
},
"next": {
"href": "http://myapi.rest.com/someEndpoint?offset=60&limit=20"
},
"prev": {
"href": "http://myapi.rest.com/someEndpoint?offset=20&limit=20"
},
"last": {
"href": "http://myapi.rest.com/someEndpoint?offset=14640&limit=20"
},
"first": {
"href": "http://myapi.rest.com/someEndpoint?offset=0&limit=20"
}
},
"page": {
"size": 20,
"totalElements": 14659,
"totalPages": 733,
"number": 2
}
}
The Traverson code that I use to consume this is
public PagedResources<Resource<SomePojoVO>> getSomePojo(){
Traverson traverson = new Traverson(URI.create(url), MediaTypes.HAL_JSON);
ParameterizedTypeReference<PagedResources<Resource<SomePojoVO>>> pojoVOs =
new ParameterizedTypeReference<PagedResources<Resource<SomePojoVO>>>() {};
return traverson.follow("$._links.self.href").toObject(pojoVOs);
}
When looking at the access logs for myapi.rest.com
. I notice that two invocations are made. One is when the Traverson
object is created and the other when follow()
is invoked. I've also tried to do the following
traverson.follow("$._embedded").toObject(pojoVOs);
but I get an UriSyntaxException
with the message Illegal character in scheme name at index 0: {
thrown at TraversonBuilder.traverseToExpandedFinalUrl
where it tries to invoke the code new UriTemplate(uri)
with uri
resulting in the value of _embedded
.
I feel like I am doing something wrong here, as having to invoke the endpoint twice to try to get PagedResources<Resource<SomePojoVO>>
seems incorrect.
Does anyone have any pointers about what I might be doing wrong here?
Some other information. I am using Spring HATEOAS v 0.25.2, and I am not able to upgrade at this point.
Thank you for your responses/help.