How to bind custom Hateoas link class to RestTemplate?

删除回忆录丶 提交于 2019-12-13 04:41:15

问题


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.

来源:https://stackoverflow.com/questions/54427139/how-to-bind-custom-hateoas-link-class-to-resttemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!