I can follow the links with spring traveson HAL library, I wonder if is there any way that I can follow an embedded link as well.
for example, I can follow someLink with traverson.follow("someLink") but I am looking for a way to follow someResource self link.
{
"name": "some name",
"_embedded": {
"someResource": [
{
"name": "some resource name",
"_links": {
"self": {
"href": "http://someEmbeddedUrl"
}
}
}
]
},
"_links": {
"someLink": {
"href": "http://someUrl"
}
}
}
I think you can use PagedResources<Resource<SomeResource>>. Take a look at the following example (SomeResource is a POJO):
Traverson.TraversalBuilder tb = traverson.follow("someLink");
ParameterizedTypeReference<PagedResources<Resource<SomeResource>>> typeRefDevices = new ParameterizedTypeReference<PagedResources<Resource<SomeResource>>>() {};
Resources<Resource<SomeResource>> resources = tb.toObject(typeRefDevices);
Resource<SomeResource> firstItem = resources.getContent().iterator().next();
firstItem.getLinks().forEach(l -> {
System.out.println(String.format("rel: %s href: %s", l.getRel(), l.getHref()));
});
For your example, this snippet should print rel: self href: http://someEmbeddedUrl
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With