【发布时间】:2020-04-12 20:47:11
【问题描述】:
我正在开发一个使用 @RepositoryRestResource 的 spring-boot 项目。
有 2 个实体,Product 和 Domain,它们具有多对多关系。
我想实现一个运行复杂查询的自定义 REST 调用。
为了实现自定义实现,我关注this blog。
ProductRepository 接口:
@RepositoryRestResource(collectionResourceRel = "product", path = "product")
public interface ProductRepository extends CrudRepository<Product, Long>, CustomProductRepository {
List<Product> findByName(@Param("name") String name);
}
CustomProductRepository.java - 自定义 REST 调用的接口
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。
我错过了什么?
谢谢。
【问题讨论】:
-
如果您有 IntelliJ IDE,请检查底部选项卡中的 spring 组件,看看您的路径是否在此处列出...
-
@silentsudo,我正在使用 intellij,但不确定在哪里可以看到弹簧组件。可以指点我,哪位能帮到我?
-
你需要编写控制器来监听你正在点击的端点。
-
@joemokenela,这不会违背使用
RepositoryRestResource的目的,我们不必编写控制器吗?
标签: java rest spring-boot jpa