2015-03-02 14 views
9

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ì?

+1

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. –

+0

Bản nháp HAL nói "Thuộc tính" được bảo lưu "_embedded" là TÙY CHỌN " –

+2

Đó 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'. –

Trả lời

5

Thêm Accept tiêu đề này để yêu cầu:

Accept : application/x-spring-data-verbose+json 
6

Khi documentation nói

application/hal + json câu trả lời phải được gửi đến yêu cầu chấp nhận application/json

Để bỏ qua _embedded trong phản hồi của bạn, bạn sẽ cần phải thêm

spring.hateoas.use-hal-as-default-json-media-type=false 

to application.properties.

Các vấn đề liên quan