2017-09-03 152 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的自定義實現,這是我的目標。 –