繁体   English   中英

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

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

我有以下问题。 我制作了一个使用spring-data的应用程序,并使用spring-data-rest将其公开为REST服务。 一切都进行得很顺利,直到我想要一个自定义的实现。 我已经使用一种其他方法创建了CustomSomethingRepository和SomethingRepositoryImpl。 Spring数据存储库接口扩展了CustomSomethingRepository,一切都很好,我可以直接从测试中执行我的方法,还可以执行自定义实现。 然后,我尝试通过REST api来获取它,在这里,我很惊讶该方法无法通过/ somethings / search使用。 我几乎百分百确定它在Spring Boot 1.3.x和JpaRepositories中可以正常工作。 现在,我正在使用引导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?

我读过这样的问题: 实现Spring Data存储库的自定义方法并通过REST公开这些方法,这表明这是不可能实现的,但是不管我信与否,我有一个带有spring-boot 1.3.x的项目以及那些实现也被暴露:)。

谢谢!

由于您的自定义方法正在返回列表,因此您应该将其放在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);
}

所以,我有和你完全一样的问题。 我有一个尚未经过全面测试的解决方案,因为我仍在努力。 我不喜欢它,因为它似乎有点笨拙……而且我还没有对其进行全面的测试。 这就是我所走的路。 在您的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的依赖性形成一个循环:

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM