Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

follow embedded link with spring traverson HAL

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"
    }
  }
}
like image 815
rawData Avatar asked Jan 17 '26 04:01

rawData


1 Answers

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

like image 134
Timur Levadny Avatar answered Jan 19 '26 20:01

Timur Levadny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!