Разрешить вложенные ссылки в POST с помощью Spring HATEOAS - PullRequest
0 голосов
/ 04 июня 2019

Я использую Spring Data REST и Spring HATEOAS в своем проекте.Я использую REST API от клиента Angular.

У меня довольно сложные модели в моем приложении, и мне интересно, правильно ли я использую HATEOAS.

Это контактный ресурс:

{
  "sid": "962732c2-68a8-413b-9762-f676d42046b4",
  "createdBy": "1ccf2329-4aa3-4d55-8878-25517edf1522",
  "createdDate": "2019-05-28T14:06:07.011Z",
  "lastModifiedDate": "2019-06-04T08:46:02.591Z",
  "lastModifiedBy": "system",
  "lastModifiedByName": null,
  "type": "CUSTOMER",
  "personType": "NATURAL_PERSON",
  "firstName": "First name",
  "lastName": "Lastname",
  "companyName": null,
  "gender": "MALE",
  "birthDate": "2019-05-21T00:00:00Z",
  "birthCity": null,
  "job": null,
  "billingAddress": "Via 123",
  "billingZipCode": "14018",
  "billingCity": "Roatto",
  "billingDistrict": "AT",
  "billingCountry": "IT",
  "shippingAddress": "Via 123",
  "shippingZipCode": "14018",
  "shippingCity": "Roatto",
  "shippingDistrict": "AT",
  "shippingCountry": "IT",
  "taxCode": "XXXXXXXXXXXXX",
  "vatNumber": null,
  "landlinePhone": null,
  "mobilePhone": null,
  "fax": null,
  "email": "aaa@sdfg.it",
  "certifiedEmail": null,
  "survey": null,
  "iban": null,
  "swift": null,
  "publicAdministration": false,
  "sdiAccountId": "0000000",
  "preset": false,
  "_links": {
    "self": {
      "href": "http://localhost:8082/api/v1/contacts/1"
    },
    "contact": {
      "href": "http://localhost:8082/api/v1/contacts/1{?projection}",
      "templated": true
    },
    "notes": {
      "href": "http://localhost:8082/api/v1/contacts/1/notes"
    },
    "auditLogs": {
      "href": "http://localhost:8082/api/v1/contacts/1/auditLogs"
    },
    "media": {
      "href": "http://localhost:8082/api/v1/contacts/1/media"
    },
    "privacyAgreements": {
      "href": "http://localhost:8082/api/v1/contacts/1/privacyAgreements"
    },
    "eyeExams": {
      "href": "http://localhost:8082/api/v1/contacts/1/eyeExams"
    },
    "contactLensEyeTests": {
      "href": "http://localhost:8082/api/v1/contacts/1/contactLensEyeTests"
    },
    "eyeExamsCount": {
      "href": "http://localhost:8082/api/v1/contacts/1/eyeExams/count"
    },
    "documents": {
      "href": "http://localhost:8082/api/v1/contacts/1/documents"
    },
    "pendingSalesOrders": {
      "href": "http://localhost:8082/api/v1/contacts/1/pendingSalesOrders"
    },
    "latestPurchasedFrames": {
      "href": "http://localhost:8082/api/v1/contacts/1/latestPurchasedFrames"
    },
    "latestPurchasedItems": {
      "href": "http://localhost:8082/api/v1/contacts/1/latestPurchasedItems"
    },
    "latestSoldItems": {
      "href": "http://localhost:8082/api/v1/contacts/1/latestSoldItems"
    },
    "latestDocuments": {
      "href": "http://localhost:8082/api/v1/contacts/1/latestDocuments"
    },
    "store": {
      "href": "http://localhost:8082/api/v1/contacts/1/store{?projection}",
      "templated": true
    }
  }
}
  1. Не слишком ли много ссылок, как в моем случае?Как я должен сказать клиенту, как ориентироваться, если я их не отправляю?Это всего лишь один ресурс, но подумайте, когда я запрашиваю постраничные результаты ... размер ответа велик (даже если я использую проекции Spring), и это в основном из-за ссылок!

  2. У клиента есть отношение store (http://localhost:8082/api/v1/contacts/1/store). Я хотел бы использовать эту ссылку в вызовах POST. Фактически, скажем, я собираюсь дублировать текущего клиента, я хотел быне звоните http://localhost:8082/api/v1/contacts/1/store, чтобы получить собственную ссылку магазина, я бы предпочел передать ссылку http://localhost:8082/api/v1/contacts/1/store в теле нового покупателя (это позволит избежать дополнительного HTTP-вызова):


{  
      "billingCountry": "IT",  
      "firstName": "First name",
      "lastName": "Last name",
      "gender": "MALE", 
      "personType": "NATURAL_PERSON", 
      "shippingCountry": "IT",
      "store": "http://localhost:8082/api/v1/contacts/1/store",
      "type": "CUSTOMER"
 }

Кажется, это невозможно из коробки, но я должен создать собственный десериализатор. Я ошибся, и не рекомендуется использовать вложенную ссылку в POST для сохраненияресурс?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...