我正在使用Spring Boot 2,Spring Data REST,Spring HATEOAS。
假设我有一个模特:
@EntityListeners({ContactListener.class})
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@Builder
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;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "store_id", updatable = false)
private Store store;
并存储:
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class Store extends AbstractEntity {
@NotBlank
@Column(nullable = false)
private String name;
@Username
@NotBlank
@Length(max = 16)
@Column(nullable = false/*, unique = true*/)
@ColumnTransformer(write = "UPPER(?)")
private String code;
private String address;
private String zipCode;
private String city;
private String district;
当我获得联系时,响应如下:
{
"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",
"createdByName": "Rossi Mario",
"lastModifiedByName": null,
"type": "CUSTOMER",
"personType": "NATURAL_PERSON",
"firstName": "Mario",
"lastName": "Rossi",
"companyName": null,
"fullName": "Rossi Mario",
"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": "XXXX",
"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
},
"store": {
"href": "http://localhost:8082/api/v1/contacts/1/store{?projection}",
"templated": true
}
}
}
如您所见,存储的链接不是资源存储的 self 链接。 我想覆盖该链接设置自身资源。所以我创建了这个处理器:
@Component
public class DocumentRowProcessor implements ResourceProcessor<Resource<Contact>> {
@Autowired
private BasePathAwareLinks service;
@Autowired
private EntityLinks entityLinks;
@Override
public Resource<Contact> process(Resource<Contact> resource) {
Store store = resource.getContent().getStore();
if(store != null){
resource.add(entityLinks.linkToSingleResource(store.getClass(), store.getId()).withRel("store"));
}
return resource;
}
}
不幸的是,该链接现在已被覆盖,但是我在“商店”内找到2个链接。调试时,我看到资源内部仅存在自我链接。我的猜测是在以下步骤中添加了相关链接。
我如何以干净的方式实现我的目标?
答案 0 :(得分:1)
http://localhost:8082/api/v1/contacts/1/store
是端点,您可以在其中检查链接到该内容的商店,也可以删除/修改这两个对象之间的关联。但是,在某些用例中,您需要自链接进行进一步的操作,并且您不想从客户端发送额外的请求。 请执行下列操作: 1.为竞争对象创建一个投影。 2.包括您需要的所有属性以及商店。 3.如果您在这里不需要商店的任何属性(仅需要自我链接),则为商店实体创建一个“空投影”,并将该投影作为商店属性包含在contact属性中。
当您获得联系人的这一预测时,结果将包含商店属性的内部内部的自链接。因此,主要的_links
集合仍将是常规的hateos链接集合,但是将有一个store._links.self.href
属性,其中将包含关联商店的自我链接。