【问题标题】:How to expose custom implementation of Spring Data Rest endpoint如何公开 Spring Data Rest 端点的自定义实现
【发布时间】:2018-02-11 18:14:19
【问题描述】:

我有以下问题。我制作了一个使用 spring-data 并使用 spring-data-rest 将其公开为 REST 服务的应用程序。一切都很顺利,直到我想要一个自定义实现。我使用一种附加方法创建了 CustomSomethingRepository 和 SomethingRepositoryImpl。 Spring数据存储库接口扩展了CustomSomethingRepository,一切都很好,我可以直接从测试中执行我的方法,自定义实现也被执行了。然后我尝试通过 REST api 获取它,在这里我很惊讶这种方法不能通过 /somethings/search 获得。我几乎百分百确定它在 spring boot 1.3.x 和 JpaRepositories 中运行良好。现在我正在使用 boot 1.5.x 和 MongoRepository。请看我的示例代码:

@RepositoryRestResource
public interface SomethingRepository extends CrudRepository<Something>, CustomSomethingRepository {

    //this one is available in /search 
    @RestResource(exported = true)
    List<Something> findByEmail(String email);
}

自定义界面

public interface CustomSomethingRepository {
    //this one will not be available in /search which is my problem :(
    List<Something> findBySomethingWhichIsNotAnAttribute();
}

和实施

@RepositoryRestResource
public class SomethingRepositoryImpl implements CustomSomethingRepository {

    @Override
    public List<Something> findBySomethingWhichIsNotAnAttribute() {
        return new ArrayList<>(); //dummy code
    }
}

您能否给我一个提示,我如何将 CustomSomethingImpl 公开为 Rest 端点的一部分,而无需创建另一个仅处理单个请求的常规 spring mvc bean?

我读过这样的问题:Implementing custom methods of Spring Data repository and exposing them through REST 声明这是不可能实现的,但信不信由你,我有一个带有 spring-boot 1.3.x 的项目,并且这些实现也被公开了 :) .

谢谢!

【问题讨论】:

  • 也许我的howto 会有所帮助..
  • 您好,谢谢您的回答,但它没有公开自定义实现,您正在创建另一个控制器,这是一种解决方法,我想说。问题是如何自动公开自定义实现,这可以从 Spring 数据级别获得。
  • 我的注意力不集中..))
  • 由于您的自定义方法返回一个 List 您应该将它放在Something Repository 中,spring 数据将把它放在/search 路径上。添加列表 findByNotAttribute()

标签: java spring rest spring-data spring-data-rest


【解决方案1】:

由于您的自定义方法返回一个列表,您应该将它放在SomethingRepository 中,spring 数据休息会将它放在/search 路径上。添加列表 findByNotAttribute()

@RepositoryRestResource public interface SomethingRepository extends CrudRepository<Something> { 
@RestResource(exported = true)
List<Something> findByEmail(String email);

List<Something> findByNotAttribute(@Param String attribute);
}

【讨论】:

  • 是的,但它不会使用来自 SomethingRepositoryImpl 的自定义实现,这是我的目标。
  • @MichalPlewka 当方法由@Query 注释恕我直言时,您不应该提供方法主体。在您的界面中尝试方法名称、返回类型和注释,例如您有findByEmail。例如,请参阅docs.spring.io/spring-data/jpa/docs/current/reference/html/…
【解决方案2】:

所以,我和你有完全相同的问题。我有一个未经过全面测试的解决方案,因为我仍在研究它。我不喜欢它,因为它似乎有点hacky......而且我还没有完全测试它。这就是我已经走了多远。在您的 CustomSomethingRepository 中,将 @Query 注释添加到您要公开的方法中。在注释内添加一个有效的查询。

public interface CustomSomethingRepository {
     @Query("select smt from Something smt")
     List<Something> findBySomethingWhichIsNotAnAttribute();

现在在实现您的 CustomSomethingRepository 的类中

@Repositorypublic
@Transactional(readOnly = true)
class SomethingRepositoryImpl implements CustomSomethingRepository {

     @PersistenceContext
     private EntityManager entityManager;

     @Override
     public List<Something> findBySomethingWhichIsNotAnAttribute() {
          System.out.println("HELLO");
     }
 }

现在,当您转到 http://localhost/something/search 时,您应该会看到类似

{
  "_links" : {
    "findByEmail" : {
      "href" : "http://localhost/something/search/findByEmail{?email}"
    },
    "findBySomethingWhichIsNotAnAttribute" : {
      "href" : "http://localhost/something/search/findBySomethingWhichIsNotAnAttribute"
    },
    "self" : {
      "href" : "http://localhost/something/search/"
    }
  }
}

当您将浏览器指向 http://localhost/something/search/findBySomethingWhichIsNotAnAttribute 时,您将看到打印的 HELLO,并且 @Query 注释内的查询将运行。

我面临另一个问题。在SomethingRepositoryImpl中,我希望能够调用SomethingRepository中的findAll()方法,但是如果我将SomethingRepository自动连接到SomethingRepositoryImpl,应用程序会出错,因为它检测到一个循环。

应用上下文表单中一些bean的依赖关系 一个循环:

【讨论】:

猜你喜欢
  • 2018-03-17
  • 2015-05-03
  • 2014-11-27
  • 2016-03-26
  • 2014-11-13
  • 2016-04-30
  • 2016-03-06
  • 2023-03-09
  • 2013-06-06
相关资源
最近更新 更多