-7

I have this old Spring Hateos code which I want to migrate to the latest version:

    Map<String, Link> links = new HashMap<>();

    links.put(Link.REL_NEXT, response.getLink(Link.REL_NEXT));
    links.put(Link.REL_PREVIOUS, response.getLink(Link.REL_PREVIOUS));

    addLink(url, response, links, Link.REL_NEXT);
    addLink(url, response, links, Link.REL_PREVIOUS);

  ....

    private void addLink(String baseUrl, WebResource response, Map<String, Link> links, String rel) {
    if (links.get(rel) == null) {
      return;
    }
    Link link = links.get(rel);
    String href = baseUrl;
    if (link.getHref().contains("?")) {
      href = href + link.getHref().substring(link.getHref().indexOf('?'));
    }
    link = Link.of(href, rel);
    response.add(link);
  }

I tried this:

    Map<LinkRelation, Optional> links = new HashMap<>();


    links.put(IanaLinkRelations.SELF, response.getLink(IanaLinkRelations.SELF));

    links.put(IanaLinkRelations.NEXT, response.getLink(IanaLinkRelations.NEXT));

    links.put(IanaLinkRelations.PREVIOUS, response.getLink(IanaLinkRelations.PREVIOUS));


    addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.SELF);

    addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.NEXT);

    addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.PREVIOUS);


private void addLink(String baseUrl, RegistrationsResource response, Map<LinkRelation, Optional> links, LinkRelation rel) {
    if (links.get(rel) == null) {
      return;
    }
    Link link = links.get(rel);
    String href = baseUrl;
    if (link.getHref().contains("?")) {
      href = href + link.getHref().substring(link.getHref().indexOf('?'));
    }
    link = Link.of(href, rel);
    response.add(link);
  }

I get error at this line:

Link link = links.get(rel);

Required type:  Link
Provided:       Optional

Can you advise what is the correct way to implement this?

Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

1 Answers1

1

Based on your comments & question for migration this is what I am suggesting:

Map<LinkRelation, Optional<Link>> links = new HashMap<LinkRelation, Optional<Link>>();

links.put(IanaLinkRelations.SELF, Optional.of(response.getLink(IanaLinkRelations.SELF)));

links.put(IanaLinkRelations.NEXT, Optional.of(response.getLink(IanaLinkRelations.NEXT)));

links.put(IanaLinkRelations.PREVIOUS, Optional.of(response.getLink(IanaLinkRelations.PREVIOUS)));

....

//calling addLinlk

addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.SELF);

addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.NEXT);

addLink(apmCoreBaseUrl, response, links, IanaLinkRelations.PREVIOUS);

And inside addLink:

private void addLink(String baseUrl, RegistrationsResource response, Map<LinkRelation, Optional> links, LinkRelation rel) {

    links.get(rel).ifPresent(x->{
     Link link = x;
     String href = baseUrl;
    if (link.getHref().contains("?")) {
      href = href + link.getHref().substring(link.getHref().indexOf('?'));
    }
    link = Link.of(href, rel);
    response.add(link);
    });
  }

Tested with Java 11 & spring-hateoas 1.5.0. If you have any different versions, please let me know.

Edit

As per OP mentioned, they are using hateoas version 2.6.7. The response reference in OP's code is custom class which extending RepresentationModel. So response.getLink(..) will return type of Optional<Link>. So below workaround will work :

Map<LinkRelation, Optional<Link>> links = new HashMap<LinkRelation, Optional<Link>>();

links.put(IanaLinkRelations.SELF, response.getLink(IanaLinkRelations.SELF));

...

No changes in addLink & no changes in calling addLink required, my original answer still valid for other operations.

Ashish Patil
  • 4,428
  • 1
  • 15
  • 36
  • Thank you for the answer. I made a small change like this: https://pastebin.com/Vt8GJyWV I use Java 17 and `org.springframework.boot:spring-boot-starter-hateoas:2.6.7` Can you advise is this valid change, please? If possible can you test it, please? – Peter Penzov May 24 '22 at 01:09
  • @PeterPenzov, sure let me have a look. – Ashish Patil May 24 '22 at 07:22
  • @PeterPenzov, I have 2 questions : 1. can you please give us details of `RegistrationnResource` class? From which library you are using this. Or is this your custom class? 2. You put `response.getLink(..)` directly to HashMap, you can you please share what is class type of `response` here? Once we have these details, it may be pretty straight forward to resolve your issue. – Ashish Patil May 24 '22 at 08:08
  • Here it is: https://pastebin.com/95UJATMV – Peter Penzov May 24 '22 at 09:40
  • Ok, will have a look & keep you posted, if require, we can start a chat conversation later. – Ashish Patil May 24 '22 at 09:55
  • @PeterPenzov, `RegistrationsResource` you shared does not have `add` & `getLink` method. Does that mean it is part of super class which `RegistrationsResource` is using? The reason I am asking this question because I just want to validate your `response.add(link);` & `response.getLink` method if they are compatible to what you are achieving. – Ashish Patil May 24 '22 at 12:03
  • Moreover, can you please let me know what error you got if you use my code as it is ? will it give you some error? – Ashish Patil May 24 '22 at 12:06
  • Here are the extended methods: https://pastebin.com/hPj3B4wK and for `getLink` I that this one is used https://github.com/spring-projects/spring-hateoas/blob/main/src/main/java/org/springframework/hateoas/RepresentationModel.java#L241 and https://github.com/spring-projects/spring-hateoas/blob/main/src/main/java/org/springframework/hateoas/RepresentationModel.java#L106 – Peter Penzov May 24 '22 at 12:13
  • I can't run this code right now because I don't have a test environment. – Peter Penzov May 24 '22 at 12:13
  • Hi @PeterPenzov, I just verified & I can see your code will work, I am modifying my answer to support that evidence. – Ashish Patil May 24 '22 at 12:18
  • Thank you! A well deserved bounty score. – Peter Penzov May 24 '22 at 12:24
  • Thanks, I am honored, I just edited my answer which will support evidence that `response.getLink(..)` will work. Please let me know if there any error you faced while running your code, will check on that. – Ashish Patil May 24 '22 at 12:25