gpt4 book ai didi

java - @RepositoryRestResource 中的自定义实现

转载 作者:行者123 更新时间:2023-11-29 02:58:15 25 4
gpt4 key购买 nike

我正在开发一个使用 @RepositoryRestResource 的 spring-boot 项目。

有2个实体,Product & Domain,它们是多对多的关系。

我想实现一个运行复杂查询的自定义 REST 调用。

为了实现自定义实现,我正在关注 this blog .

ProductRepository 接口(interface):

@RepositoryRestResource(collectionResourceRel = "product", path = "product")
public interface ProductRepository extends CrudRepository<Product, Long>, CustomProductRepository {

List<Product> findByName(@Param("name") String name);

}

CustomProductRepository.java - 自定义 REST 调用的接口(interface)

public interface CustomProductRepository {
public List<Product> findByDomainName(String domainName);
}

ProductRepositoryImpl.java - 它是实现。 (我遵循博客中提到的命名约定)

public class ProductRepositoryImpl implements CustomProductRepository {

@Override
public List<Product> findByDomainName(long id, String domainName) {
List<Product> result = new ArrayList<>();
// Query logic goes here
return result;
}
}

更新 ProductRepository.java 以扩展 CustomProductRepository

@RepositoryRestResource(collectionResourceRel = "product", path = "product")
public interface ProductRepository extends CrudRepository<Product, Long>, CustomProductRepository {

List<Product> findByName(@Param("name") String name);

}

应用程序启动时没有错误。我希望当我调用 http://localhost:8080/product/search 时,它应该列出我的自定义方法:findByDomainName。但事实并非如此。

我得到的是:

{
"_links" : {
"findByName" : {
"href" : "http://localhost:8080/product/search/findByName{?name}",
"templated" : true
},
"self" : {
"href" : "http://localhost:8080/product/search/"
}
}
}

即使我调用 REST 调用 http://localhost:8080/product/search/findByDomainName ,我得到 404

我错过了什么?

谢谢。

最佳答案

显然这是不可能的设计。如果使用 @Query 进行注释,自定义方法将起作用。参见 implementing-custom-methods-of-spring-data-repository-and-exposing-them...

关于java - @RepositoryRestResource 中的自定义实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59423534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com