[英]How to create a client for a spring boot service?
我基於此示例為Spring Boot服務提供了一個簡單的CRUD API。
該API具有以下定義:
@RepositoryRestResource
public interface PersonRepository extends CrudRepository<Person, Long> {}
從curl客戶端,我可以使用以下隱式GET
命令獲取用戶列表: curl localhost:8080/persons
。
從Java的角度來看,我們僅將Iterable<Person> persons = personRepository.findAll()
的遠程等效項稱為。
問題是spring是否可以自動創建同一API的遠程實現(客戶端), 而無需手動鍵入路徑或知道方法類型(如使用RestTemplate
時RestTemplate
)?
此類客戶端用法的示例:
PersonRepository personRepository = new PersonRepositoryClient(http://localhost:8080");
Iterable<Person> persons = personRepository.findAll(); // findAll does a remote call as if it was local.
我如何獲得這樣的客戶端“生成的”實現?
*我知道傳遞URL直接與服務發現架構相矛盾,但這只是為了了解如何到達定義良好的客戶端。 通過發現URL而不是手動設置URL,可以在服務發現體系結構中使用同一客戶端。
嘗試Feign客戶 。 它為Java接口提供了HTTP請求處理程序的自動生成。 要使其工作,您需要包括Spring Cloud,標准Spring框架沒有此功能。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.