【发布时间】:2016-05-07 14:56:21
【问题描述】:
我正在将 Spring Boot (v1.3.1) 与 Spring Data Mongo (1.8.2) 一起使用,并尝试将聚合添加到我们现有的 Mongo 存储库之一,但我在让它们一起工作时遇到了一些麻烦。
我正在引用与向单个存储库添加自定义行为相关的 Spring documentation。
在应用程序配置中,我们有以下内容:
@EnableMongoRepositories(basePackages = { "com.test" }, repositoryImplementationPostfix = "Impl")
为了说明这一点,我整理了一些测试类。我的自定义界面如下所示:
public interface TestRepositoryCustom {
TestEntity getStuff();}
我的存储库如下所示:
public interface TestRepository extends MongoRepository<TestEntity, String>, TestRepositoryCustom {
TestEntity findByName(@Param("name") String name);}
实现类在这里:
public class TestRepositoryImpl implements TestRepositoryCustom {
@Override
public TestEntity getStuff(){
System.out.println("!!!!TESTOK!!!");
return new TestEntity();
}}
当我查看搜索 URL (http://localhost:9090/testEntities/search/) 时,getStuff 方法不可用。 此外,当我访问 URL (http://localhost:9090/testEntities/search/getStuff) 时,我会收到 404。
出于绝望,我尝试更改 repositoryImplementationPostfix 并注意到如果实现类后缀和此值不同步,我会收到如下错误:
...
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property getStuff found for type TestEntity!
...
所以看起来应用上下文知道实现类,但我就是不知道我做错了什么。
任何意见将不胜感激
【问题讨论】:
-
也许应该是 TestRepositoryCustomImpl 而不是 TestRepositoryImpl?
-
您可以尝试使用其他不是 getter 的方法名称吗? Getter 应该返回属性,而不是执行数据访问。例如,尝试使用
loadStuff。 -
同样的问题我开始认为这个方法没有通过REST公开
标签: spring spring-data spring-data-mongodb spring-data-rest