Spring HATEOAS:如何通过向现有CollectionModel添加自定义链接来对其进行调整?

问题描述

在我的Spring Boot项目中,我有两个实体,例如SellerBuyer。通过扩展CrudRepository,我使Spring Data REST能够自动为每个CollectionModel创建一个/sellers,它们可以通过相应的端点(例如/buyersCollectionModel进行访问)。现在,我想通过编写一个Buyer来为RepresentationModelProcessor的{​​{1}}添加一个自定义的“过滤器”链接

@Component
public class BuyerCollectionModelProcessor implements
        RepresentationModelProcessor<CollectionModel<Buyer>> {

    @Override
    public CollectionModel<Buyer> process(CollectionModel<Buyer> model) {
        // here we add a link to some filter() method from BuyersController class
        return model.add(linkTo(methodon(BuyersController.class)
                .filterBy(Optional.empty(),Optional.empty()))
                    .withRel("filter"));
    }
}

它可以与/buyers一起使用,它现在包括“过滤器”链接

  "_links": {
        "self": {
            "href": "http://localhost:8080/buyers"
        },"filter": {
            "href": "http://localhost:8080/buyers/filterBy{?city,age}","templated": true
        }
    }

问题是,/sellers现在还具有买方“过滤器”链接

  "_links": {
        "self": {
            "href": "http://localhost:8080/sellers"
        },"templated": true
        }
    }

如果我决定以类似的方式向/sellers添加自定义链接,则/sellers/buyers将分别具有两个链接。如何修复该问题并分离自定义链接,以使它们仅包含在CollectionModel中以用于其相应实体?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)