【问题标题】:How to use a Swagger generated client (Feign)如何使用 Swagger 生成的客户端(Feign)
【发布时间】:2018-09-26 09:32:48
【问题描述】:

按照post 中的说明生成客户端库后 我试图在我的消费者中使用这个库,我添加了依赖项并在我的主类上添加了 @EnableFeignClients 注释。 但是,在启动应用程序时,Spring 抱怨存在不满足的依赖关系,从具有注释 @FeignClient 的库中生成的接口似乎没有被扫描或实例化。 包含这样一个生成的 swagger 客户端库时,是否有任何特定的配置需要做?

【问题讨论】:

  • 您解决了这个问题吗?

标签: java spring-boot swagger swagger-codegen feign


【解决方案1】:

我在同一个问题上苦苦挣扎了很长一段时间,我想分享一下我暂时解决这个问题的方法。默认情况下,swagger 将使用旧版 feign 客户端而不是新的 openfeign,因此您必须直接启用配置选项 generateForOpenFeign。此外,如果您想使用 java8,为了避免生成默认接口(什么都不做),您已将配置选项 defaultInterfaces 设置为 false。

这是我的swagger_config.json 的样子:

{
"artifactId": "example-sdk",
"artifactVersion": "1.0.0",
"groupId": "com.example",
"apiPackage": "com.example.api",
"modelPackage": "com.example.model",
"configPackage": "com.example.configuration",
"generateForOpenFeign": true,
"java8": true,
"defaultInterfaces": false
}

源文件生成后,我必须更新pom.xml中spring-cloud和spring-boot-starter的依赖关系,使de源代码可编译。

这是我更新的内容:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
</parent>

 <dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-parent</artifactId>
            <version>Greenwich.SR5</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

要使用生成的 feign 客户端,我必须把 @EnableFeignClients(basePackages = {"com.example.api"}) 在我的客户端应用程序的主类中。 我知道这不是最好的解决方案,如果有人有更好的想法,请指教。

【讨论】:

    猜你喜欢
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 2018-08-29
    • 2018-09-24
    • 1970-01-01
    • 2022-10-05
    • 2016-06-29
    相关资源
    最近更新 更多