簡體   English   中英

Spring 啟動 - rest 客戶端來自 rest Z594C103F2C6E04C3D8AB059F0310E 接口C

[英]Spring boot - rest client from rest controller interface

使用 spring 啟動 2.6.0(或更高版本)是否可以從 controller 接口生成 rest 客戶端?

我想知道是否可以像下面的用例那樣構建 spring 應用程序。

Spring 應用A需要調用spring 應用B rest接口

Spring 應用程序 B 是一個多模塊項目,可生成服務器 jar 和 api Z68995FCBF432492D150484DAC40

Spring應用A導入B的API jar

Spring application A uses controller interface from B Api jar to make a rest client based on spring annotations.

B Api jar:


   @RestsController
   public interface MyApplicationAPI {

        @GetMapping("/api/some-endpoint)
        public SomeDto someEndpoint(SomeDTO obj);

   }

B服務器jar:

   public class BApplicationAPIImpl implements MyApplicationAPI {

        
        public SomeDto someEndpoint(SomeDTO obj) {
            return xxx;

最后在 A 應用程序中:

      MyApplicationAPI restClient = Some.magic(MyApplicationAPI.class, "http://bappurl.com")
      SomeDto response = restClient.someEndpoint(param);

我相信框架 RestEasy 支持類似的方法,但你必須依賴 JAXRS 注釋。

spring 框架有類似的東西嗎? 或者甚至更好的是 spring 中已經有類似的東西 - 我更願意依賴 spring 內部庫和工具,而不是導入整個 resteasy 和 jaxrs。

Spring Framework 6(和 Spring Boot 3)將具有聲明性 HTTP 接口( 參見文檔)。 但是,它們不會使用與控制器相同的注釋,而是使用單獨的注釋。 因此,您無法為 controller 和客戶端使用相同接口的示例。

文檔中的代碼片段:

 interface RepositoryService { @GetExchange("/repos/{owner}/{repo}") Repository getRepository(@PathVariable String owner, @PathVariable String repo); // more HTTP exchange methods... }

初始化(您問題中的Some.magic()部分)可以使用WebClient完成。 從同一文檔中可以看出:

 WebClient client = WebClient.builder().baseUrl("https://api.github.com/").build(); HttpServiceProxyFactory factory = WebClientAdapter.createHttpServiceProxyFactory(client); factory.afterPropertiesSet(); RepositoryService service = factory.createClient(RepositoryService.class);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM