Skip to content

How to bind custom Hateoas link class to RestTemplate? #794

Closed
@VelDeveloper

Description

@VelDeveloper

I have a common library for a spring rest API of one my application.API exposes rest call with custom HATEOAS link.

ex)

[
  {
    "bookUid": "5c4ec057e21b840001968d31",
    "status": "PURCHASED_PENDING",
    "BookInfo": {
      ....
    },
    "_links": {
      "self": {
        "href": "http://localhost:9992/api/v1/books/5c4ec057e21b840001968d31",
        "accepts": "application/json"
      },
      "update-book": [
        {
          "href": "http://localhost:9992/api/v1/books/5c4ec057e21b840001968d31",
          "name": "ACTIVATE",
          "accepts": "application/json",
          "method": "PATCH"
        },
        {
          "href": "http://localhost:9992/api/v1/books/5c4ec057e21b840001968d31",
          "name": "CANCEL",
          "accepts": "application/json",
          "method": "PATCH"
        }
      ]
    }
  }
]

Please find the below custom link class,

public class SuperLink extends Link {

    @XmlAttribute
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String name;

    @XmlAttribute
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String accepts;

    @XmlAttribute
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String method;

    public SuperLink(Link link) {
        super(link.getHref(), link.getRel());
    }

    public SuperLink(Link link, String accepts) {
        super(link.getHref(), link.getRel());
        this.accepts = accepts;
    }

    public SuperLink(Link link, String accepts, String name, String method) {
        super(link.getHref(), link.getRel());
        this.accepts = accepts;
        this.name = name;
        this.method = method;
    }

    public String getAccepts() {
        return accepts;
    }

    public void setAccepts(String accepts) {
        this.accepts = accepts;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }
}

Client class:

public List<Resource<BookResponse>> getMyBooks(GetMyBookRequest request) throws IOException {
try {
          return List<Resource<BookResponse>> bookResponses = restOperations.exchange(builder.toUriString(), HttpMethod.GET, entity, new ParameterizedTypeReference<List<Resource<BookResponse>>>(){})
                  .getBody();
      } catch (ResourceAccessException e) {
          throw new IOException(e);
      }
}

Client sdk output:

[
  {
    "bookUid": "5c4ec057e21b840001968d31",
    "status": "PURCHASED_PENDING",
    "BookInfo": {
      ....
    },
    "_links": {
      "self": {
        "href": "http://localhost:9992/api/v1/books/5c4ec057e21b840001968d31",
      },
      "update-book": [
        {
          "href": "http://localhost:9992/api/v1/books/5c4ec057e21b840001968d31",
        },
        {
          "href": "http://localhost:9992/api/v1/books/5c4ec057e21b840001968d31",
        }
      ]
    }
  }
]

Question: How to link custom link class to RestTemplate or Jackson object Mapper? So that it will bind all the Hateoas link attribute.

Any help would be really appreciated.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions