Как связать пользовательский класс ссылок Hateoas с RestTemplate? - PullRequest
0 голосов
/ 29 января 2019

У меня есть общая библиотека для API пружинного отдыха одного моего приложения. API предоставляет вызов rest с пользовательской ссылкой HATEOAS.

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"
        }
      ]
    }
  }
]

Ниже приведена настраиваемая ссылка ниже.класс,

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;
    }
}

Класс клиента:

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);
      }
}

Выходные данные клиента SDK:

[
  {
    "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",
        }
      ]
    }
  }
]

Вопрос: Как связать пользовательский класс ссылокRestTemplate или Джексон объект Mapper?Чтобы он связывал все атрибуты ссылок Hateoas.

Любая помощь будет принята с благодарностью.

...