我有以下问题 . 我创建了一个使用spring-data的应用程序,并使用spring-data-rest将其公开为REST服务 . 一切顺利,直到我想要一个自定义实现 . 我用一个额外的方法创建了一个CustomSomethingRepository和SomethingRepositoryImpl . Spring数据存储库接口扩展了CustomSomethingRepository,一切都很好,我能够直接从test执行我的方法,也执行了自定义实现 . 然后我试图通过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 endpoints 的一部分公开而不创建另一个常规的spring mvc bean,它只是处理这个请求?
我读过这样的问题:Implementing custom methods of Spring Data repository and exposing them through REST表示这是不可能实现的,但不管你信不信,我有一个带有spring-boot 1.3.x的项目,那些实现也暴露了:) .
谢谢!
2 回答
由于您的自定义方法返回一个List,您应该将它放在SomethingRepository中,Spring数据将把它放在/ search路径上 . 添加列表findByNotAttribute()
所以,我和你有完全相同的问题 . 我有一个没有经过充分测试的解决方案,因为我仍在努力 . 我不喜欢它,因为它似乎有点hacky ...我还没有完全测试它 . 这是我走了多远 . 在CustomSomethingRepository中,将@Query注释添加到要公开的方法中 . 在注释内添加有效查询 .
现在在实现CustomSomethingRepository的类中
现在,当你去http://localhost/something/search时,你会看到类似的东西
当您将浏览器指向http://localhost/something/search/findBySomethingWhichIsNotAnAttribute时,您将看到HELLO已打印,并且@Query注释中的查询将运行 NOT .
我正面临另一个问题 . 在SomethingRepositoryImpl中,我希望能够在SomethingRepository中调用findAll()方法,但如果我将SomethingRepository自动装配到SomethingRepositoryImpl,则应用程序会因为检测到循环而错误输出 .