gpt4 book ai didi

java - 如何记录剩余文档中的链接

转载 作者:行者123 更新时间:2023-12-01 21:32:54 25 4
gpt4 key购买 nike

我正在尝试记录使用 Spring Rest 文档返回所有者列表的方法,但我不知道如何记录 Hateoas 提供的链接。

linkWithRel(...) 不适用于 _embedded.ownerDtoList 对象。

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class OwnerDto extends RepresentationModel<OwnerDto> {

@Null
private UUID id;

@NotBlank
@Size(min = 3, max = 20)
private String name;

}

@GetMapping(value = "")
public ResponseEntity<CollectionModel<OwnerDto>> getOwners() {
List<OwnerDto> owners = ownerService.findAllOwners();
HttpHeaders headers = new HttpHeaders();
headers.add("X-Owners-Total", Integer.toString(owners.size()));
owners.stream().map(owner -> owner
.add(linkTo(OwnerController.class).slash(owner.getId()).withSelfRel())
.add(linkTo(OwnerController.class).withRel("owners")))
.collect(Collectors.toList());

Link mainSelfLink = linkTo(OwnerController.class).withSelfRel();
return new ResponseEntity<>(
new CollectionModel<>(owners, mainSelfLink),
headers,
HttpStatus.OK
);
}



public void findAllOwners() throws Exception {

List<OwnerDto> ownerDtoList = Arrays.asList(ownerDto_1, ownerDto_2);
Mockito.when(ownerService.findAllOwners()).thenReturn(ownerDtoList);

mockMvc.perform(get("/api/v1/owner")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$._embedded.ownerDtoList[0].id", is(ownerDto_1.getId().toString())))
.andExpect(jsonPath("$._embedded.ownerDtoList[0].name", is(ownerDto_1.getName())))
.andExpect(jsonPath("$._embedded.ownerDtoList[1].id", is(ownerDto_2.getId().toString())))
.andExpect(jsonPath("$._embedded.ownerDtoList[1].name", is(ownerDto_2.getName())))
.andExpect(header().longValue("X-Owners-Total", 2L))
.andDo(document("v1/{method-name}", ownerPageHeadersSnippet(), ownerCollectionResponseFieldsSnippet(),

links(
halLinks(),
linkWithRel("owners").description("Get all owners <<Resource>>"),
linkWithRel("self").description("Self <<Resource>>")
)
));
}

{
"_embedded": {
"ownerDtoList": [
{
"id": "9eccbed8-6184-470c-b635-7d7bd4196caf",
"name": "Szymaa",
"_links": {
"self": {
"href": "http://localhost:8088/api/v1/owner/9eccbed8-6184-470c-b635-7d7bd4196caf"
},
"owners": {
"href": "http://localhost:8088/api/v1/owner"
}
}
},
{
"id": "f0edf088-d1ff-49dc-9561-e65ab0dcd645",
"name": "dsad23",
"_links": {
"self": {
"href": "http://localhost:8088/api/v1/owner/f0edf088-d1ff-49dc-9561-e65ab0dcd645"
},
"owners": {
"href": "http://localhost:8088/api/v1/owner"
}
}
}
]
},
"_links": {
"self": {
"href": "http://localhost:8088/api/v1/owner"
}
}

}

org.springframework.restdocs.snippet.SnippetException: Links with the following relations were not found in the response: [owners]

最佳答案

Spring REST 文档不支持开箱即用。建议的方法是链接到嵌入资源的文档,而不是将嵌入资源的链接记录为嵌入该资源的每个资源的文档的一部分。在您的情况下,这将是指向 ownerDtoList 资源文档的链接(我怀疑它可以作为 http://localhost:8088/api/v1/owner 提供)。

如果您希望在嵌入的资源链接的各处记录它们,则可以实现自定义 LinkExtractor 并使用它代替 halLinks()

您可以在 this Spring REST Docs issue 中了解有关上述内容的更多信息。 .

关于java - 如何记录剩余文档中的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58810129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com