3

如何配置Spring Data REST以删除Repository 接口端点的 Collection 资源响应上的实体关联链接(仅留下“self”),而不在 @ResResource 注释上设置exported=false (需要保持导出端点)

我们有_links部分在响应中具有最大大小的实体:

  • 项目资源_链接对于浏览关联很有用。

  • 但是在Collection Resources上,主要是在大型集合上,这些信息并不重要,并且会使响应变得不必要地更大。

我们需要更改此响应:

    {
     "_embedded" : {
     "persons" : [ {
       "id" : "bat_3191",
       "name" : "B",
       "_links" : {     // 80% of response size !!
            "self" : {
                "href" : "http://localhost:8080/api/persons/bat_3191"
            },
            "orders" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/order"
            },
            "payments" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/payments"
            },
            "childrens" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/childrens"
            },
            "invoices" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/invoices"
            },
            "brands" : {
                "href" : "http://localhost:8080/api/persons/bat_3191/brands"
            },
        }
      },
      { person [2] } 
        ... 
      { person [N] }
      ]
    },
      "_links" : {
       [page links]
    }

对于 _links 部分的唯一“自我”:

{
"_embedded" : {
"persons" : [ {
  "id" : "bat_3191",
  "name" : "B",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/persons/bat_3191"
    }
  }
}, {
  "id" : "bat_2340",
  "name" : "B",
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/api/persons/bat_2340"
    }
  }
4

1 回答 1

0

如果我想控制 springboot 中的 hatos 链接,我使用了资源汇编器。举个例子:

@Autowired
private EmployeeAddressResourceAssembler assembler;

@GetMapping(value="/{empId}", produces=MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<EmployeeAddressResource> getEmployeeAddress(@PathVariable Integer empId) {
    EmployeeAddressItem employeeAddressItem = restTemplate.getForObject( 
        serviceUrl + "/employee/address/{empId}", 
        EmployeeAddressItem.class, empId);
    return ResponseEntity.ok( assembler.toResource(employeeAddressItem) );
}

然后我使用了资源汇编器。

@Component
public class EmployeeAddressResourceAssembler
        extends ResourceAssemblerSupport<EmployeeAddressItem, EmployeeAddressResource> {

    public EmployeeAddressResourceAssembler() {
        super(EmployeeAddressController.class, EmployeeAddressResource.class);
    }

    @Override
    public EmployeeAddressResource toResource(EmployeeAddressItem item) {

        // createResource(employeeAddressItem);
        EmployeeAddressResource resource = createResourceWithId(item.getEmpId(), item);
        resource.fromEmployeeAddressItem(item);
        // … do further mapping
        resource.add(linkTo(methodOn(EmployeeAddressController.class).deleteEmployeeAddress(item.getEmpId())).withRel("delete"));        
        return resource;
    }

}

并且 ResourceAssembler 使用 Resource

public class EmployeeAddressResource extends ResourceSupport {
    private Integer empId;
    private String address1;
    private String address2;
    private String address3;
    private String address4;
    private String state;
    private String country;

    public void fromEmployeeAddressItem(EmployeeAddressItem item) {
        this.empId = item.getEmpId();
        this.address1 = item.getAddress1();
        this.address2 = item.getAddress2();
        this.address3 = item.getAddress3();
        this.address4 = item.getAddress4();
        this.state = item.getState();
        this.country = item.getCountry();
    }

所有这一切都在RestPracticeWithHateos

于 2019-06-19T17:17:18.333 回答