使用Spring HATEOAS解决POST中的嵌套链接

时间:2019-06-04 17:38:21

标签: spring spring-data-rest spring-hateoas

我在项目中使用Spring Data REST和Spring HATEOAS。我正在从Angular客户端使用REST API。

我的应用程序中有非常复杂的模型,而且一直想知道我是否以正确的方式使用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. 客户有一个关系storehttp://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中使用嵌套链接来保存资源不是一个好习惯吗?

0 个答案:

没有答案