Java SpringHateOAS:如何通过添加自定义链接来调整现有的CollectionModel?
在我的Spring Boot项目中,我有两个实体,如Java SpringHateOAS:如何通过添加自定义链接来调整现有的CollectionModel?,java,spring,spring-data-rest,spring-hateoas,Java,Spring,Spring Data Rest,Spring Hateoas,在我的Spring Boot项目中,我有两个实体,如卖方和买方。通过扩展crudepository,我使SpringDataREST能够自动为它们中的每一个创建一个CollectionModel,可以通过相应的端点(如/sellers和/bullers)访问。现在,我想通过编写RepresentationModelProcessor这样的RepresentationModelProcessor为Buyer添加一个自定义“过滤器”-链接到CollectionModel: @组件 公共类BuyerC
卖方
和买方
。通过扩展crudepository
,我使SpringDataREST能够自动为它们中的每一个创建一个CollectionModel
,可以通过相应的端点(如/sellers
和/bullers
)访问。现在,我想通过编写RepresentationModelProcessor
这样的RepresentationModelProcessor
为Buyer
添加一个自定义“过滤器”-链接到CollectionModel
:
@组件
公共类BuyerCollectionModelProcessor实现
RepresentationModelProcessor{
@凌驾
公共集合模型流程(集合模型){
//这里我们添加了一个链接,指向BuyerController类中的某个filter()方法
返回model.add(linkTo(methodOn)(BuyerController.class)
.filterBy(可选.empty(),可选.empty())
.withRel(“过滤器”);
}
}
它与预期的/bullers
一起工作,现在包括“过滤器”链接:
“\u链接”:{
“自我”:{
“href”:”http://localhost:8080/buyers"
},
“过滤器”:{
“href”:”http://localhost:8080/buyers/filterBy{?城市,时代}“,
“模板化”:正确
}
}
然而,问题是,/sellers
现在也有买家“筛选”链接:
“\u链接”:{
“自我”:{
“href”:”http://localhost:8080/sellers"
},
“过滤器”:{
“href”:”http://localhost:8080/buyers/filterBy{?城市,时代}“,
“模板化”:正确
}
}
如果我决定以类似的方式向/sellers
添加自定义链接,那么/sellers
和/bullers
将分别具有两个链接。如何修复该链接并分离自定义链接,以便它们仅包含在其相应实体的CollectionModel
中