简体   繁体   English

Spring 启动 - rest 客户端来自 rest Z594C103F2C6E04C3D8AB059F0310E 接口C

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

With spring boot 2.6.0 (or above) is it possible to generate a rest client from aa controller interface?使用 spring 启动 2.6.0(或更高版本)是否可以从 controller 接口生成 rest 客户端?

Im wondering if its possible to build spring application like this following use case.我想知道是否可以像下面的用例那样构建 spring 应用程序。

Spring application A needs to call spring application B rest interface Spring 应用A需要调用spring 应用B rest接口

Spring application B is a multimodule project that produces server jar, and a api jar Spring 应用程序 B 是一个多模块项目,可生成服务器 jar 和 api Z68995FCBF432492D150484DAC40

Spring application A imports the B's API jar Spring应用A导入B的API jar

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

B Api jar: B Api jar:


   @RestsController
   public interface MyApplicationAPI {

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

   }

B server jar: B服务器jar:

   public class BApplicationAPIImpl implements MyApplicationAPI {

        
        public SomeDto someEndpoint(SomeDTO obj) {
            return xxx;

And finally within A application:最后在 A 应用程序中:

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

I believe that framework RestEasy supports similar approach, but you have to rely on JAXRS annotations.我相信框架 RestEasy 支持类似的方法,但你必须依赖 JAXRS 注释。

Is there anything like that for spring framework? spring 框架有类似的东西吗? Or even better is there anything like this within spring already - i would prefer to rely on spring inhouse libraries and tools, rather than importing entire resteasy and jaxrs.或者甚至更好的是 spring 中已经有类似的东西 - 我更愿意依赖 spring 内部库和工具,而不是导入整个 resteasy 和 jaxrs。

Spring Framework 6 (and Spring Boot 3) will have declarative HTTP interfaces ( see documentation ). Spring Framework 6(和 Spring Boot 3)将具有声明性 HTTP 接口( 参见文档)。 However, they won't use the same annotations as controllers, but separate ones.但是,它们不会使用与控制器相同的注释,而是使用单独的注释。 So your example where you use the same interface for both controller and client won't be possible.因此,您无法为 controller 和客户端使用相同接口的示例。

Code snippet from the documentation:文档中的代码片段:

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

Initialization (the Some.magic() part in your question) can be done with WebClient .初始化(您问题中的Some.magic()部分)可以使用WebClient完成。 As can be seen in the same documentation:从同一文档中可以看出:

 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