【问题标题】:Custom implementation in @RepositoryRestResource@RepositoryRestResource 中的自定义实现
【发布时间】:2020-04-12 20:47:11
【问题描述】:

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

有 2 个实体,ProductDomain,它们具有多对多关系。

我想实现一个运行复杂查询的自定义 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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-06-15
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-31
    • 1970-01-01
    • 2014-02-01
    相关资源
    最近更新 更多