简体   繁体   English

如何公开Spring Data Rest端点的自定义实现

[英]How to expose custom implementation of Spring Data Rest endpoint

I have a following problem. 我有以下问题。 I made an application which uses spring-data and exposes it as a REST service using spring-data-rest. 我制作了一个使用spring-data的应用程序,并使用spring-data-rest将其公开为REST服务。 Everything went smooth till I wanted to have a custom implementation. 一切都进行得很顺利,直到我想要一个自定义的实现。 I've created a CustomSomethingRepository and SomethingRepositoryImpl with one additional method. 我已经使用一种其他方法创建了CustomSomethingRepository和SomethingRepositoryImpl。 Spring data repository interface extended CustomSomethingRepository and everything was fine, I was able to execute my method from test directly, custom implementation was executed as well. Spring数据存储库接口扩展了CustomSomethingRepository,一切都很好,我可以直接从测试中执行我的方法,还可以执行自定义实现。 Then I tried to get it through REST api and here I was surprised that this method is not available through /somethings/search . 然后,我尝试通过REST api来获取它,在这里,我很惊讶该方法无法通过/ somethings / search使用。 I'm almost hundred percent sure that it worked fine in spring boot 1.3.x and JpaRepositories. 我几乎百分百确定它在Spring Boot 1.3.x和JpaRepositories中可以正常工作。 Now I'm using boot 1.5.x and MongoRepository. 现在,我正在使用引导1.5.x和MongoRepository。 Please take a look at my example code: 请看一下我的示例代码:

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

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

and custom interface 和自定义界面

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

and implementation 和实施

@RepositoryRestResource
public class SomethingRepositoryImpl implements CustomSomethingRepository {

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

Could you please give me a hint how can I expose CustomSomethingImpl as a part of Rest endpoint without creating another regular spring mvc bean which will be just handling this single request? 您能否给我一个提示,如何将CustomSomethingImpl公开为Rest端点的一部分,而无需创建另一个仅将处理此单个请求的常规spring mvc bean?

I've read questions like this: Implementing custom methods of Spring Data repository and exposing them through REST which state that this is not possible to achieve, but believe me or not, I had a project with spring-boot 1.3.x and those implementations were exposed as well :). 我读过这样的问题: 实现Spring Data存储库的自定义方法并通过REST公开这些方法,这表明这是不可能实现的,但是不管我信与否,我有一个带有spring-boot 1.3.x的项目以及那些实现也被暴露:)。

Thank you! 谢谢!

Since your custom method is returning a List you should put it in SomethingRepository which spring data rest will put it on the /search path. 由于您的自定义方法正在返回列表,因此您应该将其放在SomethingRepository中,该列表中的spring数据将其放在/ search路径中。 Add List findByNotAttribute() 添加列表findByNotAttribute()

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

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

So, I have the exact same question as you. 所以,我有和你完全一样的问题。 I have a not fully tested solution because Im still working on it. 我有一个尚未经过全面测试的解决方案,因为我仍在努力。 I don't like it because it seems to be a bit hacky...Also I haven't tested it out fully. 我不喜欢它,因为它似乎有点笨拙……而且我还没有对其进行全面的测试。 This is how far I have gotten. 这就是我所走的路。 In your CustomSomethingRepository add the @Query annotation to the method you want to expose. 在您的CustomSomethingRepository中,将@Query批注添加到要公开的方法中。 Inside the annotation add a valid query. 在注释内添加一个有效的查询。

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

Now in the class that implements your CustomSomethingRepository 现在在实现CustomSomethingRepository的类中

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

     @PersistenceContext
     private EntityManager entityManager;

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

Now when you go to http://localhost/something/search you should see something like 现在,当您转到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/"
    }
  }
}

When you point your browser to http://localhost/something/search/findBySomethingWhichIsNotAnAttribute you will see HELLO printed and the query inside the @Query annotation will NOT run. 当您将浏览器指向http:// localhost / something / search / findBySomethingWhichIsNotAnAttribute时,您将看到HELLO打印,并且@Query批注内的查询将不会运行。

I'm facing another problem. 我正面临另一个问题。 In the SomethingRepositoryImpl I want to be able to call the findAll() method(s) in the SomethingRepository but if I autowire the SomethingRepository to SomethingRepositoryImpl the application errors out because it detects a cycle. 在SomethingRepositoryImpl中,我希望能够在SomethingRepository中调用findAll()方法,但是如果我将SomethingRepository自动连接到SomethingRepositoryImpl,则应用程序会因为检测到周期而出错。

The dependencies of some of the beans in the application context form a cycle: 应用程序上下文中某些bean的依赖性形成一个循环:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Spring数据REST公开自定义DTO crud存储库? - How to expose custom DTO crud repository with Spring data REST? 如何在Spring Data REST中公开@EmbeddedId转换器 - How to expose @EmbeddedId converters in Spring Data REST 如何使用Spring Data REST仅公开可写的REST API? - How to expose only writable REST api with Spring Data REST? 如何通过具有弹簧数据休息的备用键公开实体 - How to expose an entity via alternate keys with spring data rest Spring Data Rest 将自定义端点添加到特定的存储库 - Spring Data Rest Add custom endpoint to specific reposiotry Spring和CXF同时公开一个端点的休息和肥皂 - Expose Rest and Soap for one endpoint simultaneously by Spring and CXF Spring Data Rest和Spring Data Envers:如何为扩展Revision Repository的Repository公开REST API - Spring Data Rest & Spring Data Envers: How to expose REST API for Repository that extends Revision Repository 如何将 spring-data-rest 与 spring websocket 混合到一个实现中 - How to mix spring-data-rest with spring websocket into a single implementation 如何在Spring Data REST导出的端点中使用列表? - How to use List in endpoint exported by Spring Data REST? 如何使用Spring Data REST创建自定义PersistentEntityResourceAssembler - How to create custom PersistentEntityResourceAssembler with Spring Data REST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM