[英]Spring HATEOAS wrong self link with linked resources
我正在使用Spring Boot 2.0.3,Spring Data REST,Spring HATEOAS。 我的域名模型非常有條理,但最近我在自我鏈接中發現了一個奇怪的行為。
我將展示模型的一部分以指出問題,刪除無用的部分:
EyeExam:
@EntityListeners(value = EyeExamListener.class)
public class EyeExam extends AbstractEntity {
@NotNull
@JoinColumn(name = "contact_id", updatable = false)
@JsonDeserialize(using = ContactUriDeserializer.class)
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private Contact contact;
@NotNull
@Column(nullable = false, columnDefinition = "DATE")
private Instant date;
聯系:
@EntityListeners({ContactListener.class})
public class Contact extends AbstractEntity {
@NotNull
@Enumerated(EnumType.STRING)
@Column(nullable = false, columnDefinition = "VARCHAR(30) DEFAULT 'CUSTOMER'")
private ContactType type = ContactType.CUSTOMER;
@NotNull
@Enumerated(EnumType.STRING)
@Column(nullable = false, columnDefinition = "VARCHAR(30) DEFAULT 'NATURAL_PERSON'")
private PersonType personType = PersonType.NATURAL_PERSON;
private String firstName;
private String lastName;
private String companyName;
這是ContactRepository :
@Transactional
@PreAuthorize("isAuthenticated()")
public interface ContactRepository extends JpaRepository<Contact, Long> {
....
....
}
當我檢索特定的EyeExam資源( https://myserver.com/api/v1/eyeExams/13 )時,Spring返回:
{
"sid" : "f16d6e45-477f-11e9-898e-9d6f4f2f5990",
"createdBy" : "system",
"createdDate" : "2017-05-31T17:38:00Z",
"lastModifiedDate" : null,
"lastModifiedBy" : null,
"createdByName" : "System",
"lastModifiedByName" : null,
"date" : "2017-05-31T00:00:00Z",
"_links" : {
"self" : {
"href" : "https://myserver.com/api/v1/eyeExams/13"
},
"eyeExam" : {
"href" : "https://myserver.com/api/v1/eyeExams/13{?projection}",
"templated" : true
},
"supplyTypes" : {
"href" : "https://myserver.com/api/v1/eyeExams/13/supplyTypes"
},
"changeStatus" : {
"href" : "https://myserver.com/api/v1/eyeExams/13/changeStatus?status=%7Bstatus%7D"
},
"contact" : {
"href" : "https://myserver.com/api/v1/eyeExams/13/contact{?projection}",
"templated" : true
},
"store" : {
"href" : "https://myserver.com/api/v1/eyeExams/13/store{?projection}",
"templated" : true
}
}
}
如您所見,我獲得了到鏈接資源聯系人的鏈接。 沒關系。 現在我得到資源https://myserver.com/api/v1/eyeExams/13/contact和Spring回復:
{
"sid" : "4c2ba300-477e-11e9-898e-9d6f4f2f5990",
"createdBy" : "system",
"createdDate" : "2018-11-01T09:00:00Z",
"lastModifiedDate" : null,
"lastModifiedBy" : null,
"createdByName" : "System",
"lastModifiedByName" : null,
"type" : "CUSTOMER",
"personType" : "NATURAL_PERSON",
"firstName" : "John",
"lastName" : "Smith",
"companyName" : null,
"_links" : {
"self" : {
"href" : "https://myserver.com/api/v1/contact/22352"
},
"contact" : {
"href" : "https://myserver.com/api/v1/contact/22352{?projection}",
"templated" : true
},
"notes" : {
"href" : "https://myserver.com/api/v1/contacts/22352/notes"
},
"auditLogs" : {
"href" : "https://myserver.com/api/v1/contacts/22352/auditLogs"
},
"media" : {
"href" : "https://myserver.com/api/v1/contacts/22352/media"
},
"privacyAgreements" : {
"href" : "https://myserver.com/api/v1/contacts/22352/privacyAgreements"
},
"eyeExams" : {
"href" : "https://myserver.com/api/v1/contacts/22352/eyeExams"
},
"eyeExamsCount" : {
"href" : "https://myserver.com/api/v1/contacts/22352/eyeExams/count"
},
"documents" : {
"href" : "https://myserver.com/api/v1/contacts/22352/documents"
},
"pendingSalesOrders" : {
"href" : "https://myserver.com/api/v1/contacts/22352/pendingSalesOrders"
},
"lastPurchasedFrames" : {
"href" : "https://myserver.com/api/v1/contacts/22352/lastPurchasedFrames"
},
"store" : {
"href" : "https://myserver.com/api/v1/contact/22352/store{?projection}",
"templated" : true
}
}
}
我想指出自我鏈接。 這是錯誤的 ,事實上它應該是https://myserver.com/api/v1/contacts/22352的結尾。
我使用的是一些自定義ResourceProcessor,但即使沒有它們我也有同樣的問題。
現在我在ContactResourceProcessor中創建了一個解決方法,用正確的方法重寫自我鏈接,但是我想知道我做錯了什么,如果它是一個錯誤或者我錯過了什么。
這可能是一個錯誤,但嘗試這個解決方法:
@RepositoryRestResource(path="contacts")
@Transactional
@PreAuthorize("isAuthenticated()")
public interface ContactRepository extends JpaRepository<Contact, Long> {
....
....
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.