Tôi đang sử dụng Spring Boot và HATEOAS để tạo REST API và khi API của tôi trả về bộ sưu tập, nó được bao bọc bên trong một thuộc tính "_embedded", như sau:Cách xóa thuộc tính "_embedded" trong Spring HATEOAS
{
"_links":{
"self":{
"href":"http://localhost:8080/technologies"
}
},
"_embedded":{
"technologies":[
{
"id":1,
"description":"A",
"_links":{
"self":{
"href":"http://localhost:8080/technologies/1"
}
}
},
{
"id":2,
"description":"B",
"_links":{
"self":{
"href":"http://localhost:8080/technologies/2"
}
}
}
]
}
}
tôi muốn câu trả lời là như thế này:
{
"_links":{
"self":{
"href":"http://localhost:8080/technologies"
}
},
"technologies":[
{
"id":1,
"description":"A",
"_links":{
"self":{
"href":"http://localhost:8080/technologies/1"
}
}
},
{
"id":2,
"description":"B",
"_links":{
"self":{
"href":"http://localhost:8080/technologies/2"
}
}
}
]
}
TechnologiesController của tôi:
@RestController
@ExposesResourceFor(Technology.class)
@RequestMapping(value = "/technologies")
public class TechnologiesController {
...
@ResquestMapping(method = RequestMethod.GET, produces = "application/vnd.xpto-technologies.text+json")
public Resources<Resource<Technology>> getAllTechnologies() {
List<Technology> technologies = technologyGateway.getAllTechnologies();
Resources<<Resource<Technology>> resources = new Resources<Resource<Technology>>(technologyResourceAssembler.toResources(technologies));
resources.add(linkTo(methodOn(TechnologiesController.class).getAllTechnologies()).withSelfRel());
return resources;
}
Lớp cấu hình có chú thích @EnableHypermediaSupport (type = EnableHypermediaSupport.HypermediaType.HAL).
Cách tốt nhất để tạo phản hồi mà không có "_embedded" là gì?
Nếu bạn xóa '_embedded' khỏi phản hồi thì phản hồi sẽ không còn hợp lệ HAL nữa. Bạn cần phải gắn bó với '_embedded' hoặc sử dụng một loại phương tiện khác. –
Bản nháp HAL nói "Thuộc tính" được bảo lưu "_embedded" là TÙY CHỌN " –
Đó là tùy chọn trong đó tài nguyên không phải có bất kỳ tài nguyên nhúng nào. Tuy nhiên, nếu có, thì chúng phải ở dưới '_embedded'. –