【发布时间】:2019-03-16 10:46:19
【问题描述】:
我正在使用 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,结尾是-s。
我使用了一些自定义 ResourceProcessor,但即使没有它们,我也会遇到同样的问题。
现在我在 ContactResourceProcessor 中创建了一种解决方法,用正确的方法重写了自我链接,但我想了解我是否做错了什么,是错误还是我错过了什么。
【问题讨论】:
-
ContactRepository 接口上有哪些类级别的注解?
-
@Selindek 我在问题中添加了存储库。
标签: java spring spring-data-rest spring-hateoas