【问题标题】:Swagger @OpenAPIDefinition not doing anythingSwagger @OpenAPIDefinition 没有做任何事情
【发布时间】:2021-11-15 21:15:11
【问题描述】:

所以我有一个简单的 Spring Boot REST API 可以正常工作,现在我已经为 ui 和 core 添加了一些 swagger 依赖项,并使用 @ApiIgnore 或 @Operation 等标签进行了一些测试,它们都可以正常工作并且两者都在 http://localhost:8080/swagger-ui/#/

中更新

现在我正在尝试通过 @OpenAPIDefinition 标记更新 API 信息,在我的应用程序类中是这样的:

@OpenAPIDefinition(
        info = @Info(
            title = "the title",
            version = "0.0",
            description = "My API",
            license = @License(name = "Apache 2.0", url = "http://foo.bar"),
            contact = @Contact(url = "http://gigantic-server.com", name = "Fred", email = "Fred@gigagantic-server.com")))

现在我已经阅读here,Spring Boot 应用程序应该进行类扫描并识别 @OpenAPIDefinition bean 并更新 http://localhost:8080/v3/api-docs 中生成的 json。但事实并非如此,我也尝试使用 openapi.yaml 文件设置该信息,但也没有运气。

我怀疑这可能与我的依赖关系有关,因为我是新来的大摇大摆的人,仍然有点迷茫并且可能混淆了一些东西,我将 pom.xml 留在这里:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
          <groupId>io.swagger.core.v3</groupId>
          <artifactId>swagger-jaxrs2</artifactId>
          <version>2.1.2</version>
        </dependency>
        <dependency>
          <groupId>io.swagger.core.v3</groupId>
          <artifactId>swagger-jaxrs2-servlet-initializer-v2</artifactId>
          <version>2.1.2</version>
        </dependency>

        
        
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

我对如何继续感到有些茫然,并且不明白 swagger 如何识别 @ApiIgnore 标记,但忽略 @OpenAPIDefinition 标记。正如我所说,我对整个事情和 stackoverflow 都是新手,所以如果我忘记添加任何相关代码,请原谅我,谢谢!

【问题讨论】:

    标签: java spring spring-boot swagger springfox


    【解决方案1】:

    尝试删除一些不必要的依赖:

    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>3.0.0</version>
    </dependency>
    <dependency>
      <groupId>io.swagger.core.v3</groupId>
      <artifactId>swagger-jaxrs2</artifactId>
      <version>2.1.2</version>
    </dependency>
    <dependency>
      <groupId>io.swagger.core.v3</groupId>
      <artifactId>swagger-jaxrs2-servlet-initializer-v2</artifactId>
      <version>2.1.2</version>
    </dependency>
    

    springfox-boot-starter 应该已经把他们带进来了。

    如果这不起作用,您可以随时执行以下操作:

    @Configuration
    public class SwaggerConfiguration {
    
        @Bean
        public OpenAPI openAPI() {
            return new OpenAPI()
                    .info(new Info().title("the title").version("0.0").description("My API")
                        .contact(new Contact().name("Fred").url("http://gigantic-server.com").email("Fred@gigagantic-server.com")));
        }
    
    }
    

    【讨论】:

    • 删除了依赖项,似乎不起作用,当我复制该方法时,它说 a) 无法实例化类型 Contact b) 构造函数 ApiInfo(String, String, String, String, Contact, String, String, Collections.emptyList()) is undefined 我也尝试了一些其他的方法,但它们似乎不起作用,我认为这与 springfox 和 swagger 版本的混合有关,我会继续尝试!
    • 更新:我已经删除了我的 SpringFoxConfig 文件,并且现在从 v3/docs 而不是从 v2/docs 生成 swagger-ui,但是所有这些设置信息的不同方式仍然不起作用。我已经尝试在 META-INF 文件夹中设置 openapi.yaml 但仍然没有运气,我不知道从哪里继续......
    • 更新二:联系人被导入为 io.swagger.v3.oas.annotations.info.Contact;而不是 io.swagger.v3.oas.models.info.Contact;,但解决了这个问题,这个回复中的方法仍然不起作用:(
    • 像往常一样,springfox 使用起来很痛苦。两年前我们搬到springdoc,我们非常高兴。配置起来更简单,而且它确实有效。它甚至有一个migration guide from springfox。试一试;)
    【解决方案2】:

    所以在做了很多不同的事情之后,我发现了这个 guide,它解释了如何更改 API 信息,简而言之,我在 EmployeeController 类中添加了一个 @Api(tags = "Employee"),然后在我的 Application 类中添加:

    @Bean
          public Docket docket() {
            return new Docket(DocumentationType.OAS_30)
                .apiInfo(new ApiInfoBuilder()
                    .title("Employee API")
                    .description("A CRUD API to demonstrate Springfox 3 integration")
                    .version("0.0.1-SNAPSHOT")
                    .license("MIT")
                    .licenseUrl("https://opensource.org/licenses/MIT")
                    .build())
                .tags(new Tag("Employee", "Endpoints for CRUD operations on employees"))
                .select().apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                .build();
          } 
    

    这似乎解决了这个问题,我现在想知道如果我有多个控制器会发生什么,我希望这对处于困境中的任何人都有帮助,总体问题似乎是这是一种与 springfox 相关的改变 API 的方式文档和以前的方法是 swagger-core 相关的吗?正如我所说,我是这个世界的新手,所以如果有人能指出我为什么它不起作用,那就太棒了,谢谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-07
      • 2016-10-02
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多