簡體   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