【问题标题】:Remove association links for content on collection resource for Spring Data REST删除 Spring Data REST 集合资源上内容的关联链接
【发布时间】:2023-04-09 13:40:01
【问题描述】:

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

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

  • 打开Item resource _ 链接对于浏览关联非常有用。

  • 但在 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"
    }
  }

【问题讨论】:

  • 很抱歉,如果我没有解释清楚我的问题,但建议的内容不重复。我们需要删除所有实体的 JSON 的 _link 部分的关系,以减少收集请求的响应大小。我实现了重构 PersistentEntityJackson2Module 以避免杰克逊对链接进行序列化,但我相信这将是一个更好的方法。我不知道如何使用 RestRespositoryMvcConfiguration 来修改这方面。

标签: spring spring-data-rest spring-hateoas jackson2


【解决方案1】:

如果我想控制 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 使用资源

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

【讨论】:

  • 这种方法适用于 HATEOAS,但我们需要 Spring Data REST,其中 PersistentEntityResourceAssembler、PersistentEntityResource 和 RepositoryEntityController 是对资源链接转换进行建模的类。目标是如何自定义 Spring 数据 REST 的 Respositories 返回的 Collection Resources 上的实体的 Resource/links 和定义的投影。
猜你喜欢
  • 2018-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-22
  • 2017-06-21
  • 2015-11-13
  • 2014-10-08
相关资源
最近更新 更多