2017-09-03 66 views
1

我有以下问题。我制作了一个使用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。请看看我的示例代码:如何暴露Spring Data Rest端点的自定义实现

@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的项目,并且这些实现也暴露了:)。

谢谢!

+0

也许我的[HOWTO](https://stackoverflow.com/q/45401734)会有所帮助.. – Cepr0

+0

你好,谢谢你的答案,但它不公开自定义实现,你只是创建另一个控制器,这是一种解决方法,我会说。问题是如何自动公开自定义实现,这可以从spring数据级别获得。 –

+0

我不注意..)) – Cepr0

回答

0

由于您的自定义方法正在返回一个List,因此您应该将它放在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); 
} 
+0

是的,但它不会使用来自SomethingRepositoryImpl的自定义实现,这是我的目标。 –

相关问题