标签: springdoc

如何使用 Java 中的泛型制定 OpenAPI 规范

我在控制器中使用泛型。例如,从某些端点我返回Response<News>Response<Tag>

嗯,Swagger自动生成这部分yaml

responses:
        200:
          description: default response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ResponseNews'
Run Code Online (Sandbox Code Playgroud)

responses:
        200:
          description: default response
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/ResponseTags'
Run Code Online (Sandbox Code Playgroud)

这是我在 Java 中的 Response 实体。

public class Response<T> {
    private List<T> data;
    private Boolean moreDataExists;
}
Run Code Online (Sandbox Code Playgroud)

这就是 Swagger 生成组件的方式。

ResponseNews:
      type: object
      properties:
        data:
          type: array
          items:
            $ref: '#/components/schemas/News'
        moreDataExists:
          type: boolean

ResponseTags:
      type: object
      properties:
        data:
          type: array
          items:
            $ref: '#/components/schemas/Tags'
        moreDataExists:
          type: boolean
Run Code Online (Sandbox Code Playgroud)

嗯,这几乎是重复的代码。我想避免它,只在我的端点的描述中使用Response,并明确地向我的用户展示我使用泛型。

类似的东西:

responses:
        200:
          description: default …
Run Code Online (Sandbox Code Playgroud)

java swagger openapi springdoc

5
推荐指数
1
解决办法
2284
查看次数

Springdoc OpenAPI ui 不支持“位置”中的上下文路径

设置:

springdoc-openapi-ui在一个简单的 spring-boot 项目中使用1.4.0 版本(通过 Maven)中的 Java 库,没有任何自定义。

Swagger 页面在https://my-url.com/my-context-path/swagger-ui/index.html下生成

https://my-url.com/my-context-path/v3/api-docs/下的 api-docs

这两个都有效,我可以联系到他们。到现在为止还挺好!

现在的问题:

当简单地导航到https://my-url.com/my-context-path/swagger-ui.html 时,我得到一个 HTTP 状态 302 和一个location在响应头中设置的属性,它应该将我重定向到 swagger 页面从上面(我假设)。

但是,location属性中的 URL缺少上下文路径!它看起来像这样:https : //my-url.com/swagger-ui/index.html?configUrl=/v3/api-docs/swagger-config

它重定向到一个不存在的页面,我收到 404 错误代码。请注意,configUrl 似乎也缺少上下文路径。

任何想法为什么会发生这种情况以及如何解决?

这个 Github 问题似乎是同样的问题,但最后声明问题已解决:https : //github.com/springdoc/springdoc-openapi/issues/37,这是针对比我更早的版本。

swagger swagger-ui springdoc springdoc-ui springdoc-openapi-ui

5
推荐指数
2
解决办法
3906
查看次数

springdoc-openapi 在没有服务器的情况下生成 openapi yaml

我有一个 Spring boot Gradle 项目,我想获取它的 OpenAPI 规范 YAML 文件。

据我了解官方swagger-core不支持Spring boot项目,因此我发现springdoc-openapihttps://github.com/springdoc/springdoc-openapi-gradle-plugin)。

似乎为了获取 YAML/JSON 文件,在运行generateOpenApiDocs任务时,springdoc库设置了一个带有一些端点 (/v3/api-docs) 的服务器来下载文件。

  1. 我使用的是默认配置,由于某种原因,我不断收到以下错误:

任务“generateOpenApiDocs”执行失败。无法连接到 http://localhost:8080/v3/api-docs 等待了 30 秒

似乎由于某种原因它没有设置服务器。我该如何解决?

  1. 是否可以跳过服务器部分?我可以将 springdoc 配置为在构建时简单地生成文件吗?

spring spring-boot openapi springdoc

5
推荐指数
1
解决办法
1723
查看次数

以编程方式添加安全方案时,架构从组件中消失

我最近从 Springfox 转换为 Springdoc-openapi,以便为 Spring Boot Rest API 服务生成 OpenAPI。

一切都很完美,直到我添加了安全方案。一旦我这样做了,我的方案就不再出现,并且 SwaggerUI 页面上出现错误:

Could not resolve reference: Could not resolve pointer: /components/schemas/Ping does not exist in document
Run Code Online (Sandbox Code Playgroud)

我正在以编程方式设置我的配置,并且有 2 个组。

我正在使用 Spring Boot v2.4.0 和 springdoc-openapi-ui v1.5.1

我的 pom.xml 的片段:

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-ui</artifactId>
            <version>1.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-hateoas</artifactId>
            <version>1.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-security</artifactId>
            <version>1.5.1</version>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

配置片段:

    @Bean
public GroupedOpenApi apiV1() {
    String[] paths = {"/v1/**"};
    String[] packagesToScan = {"com.test.controller"};
    return GroupedOpenApi.builder()
            .group("v1")
            .packagesToScan(packagesToScan)
            .pathsToMatch(paths)
            .addOpenApiCustomiser(buildV1OpenAPI())
            .build();
}

@Bean
public GroupedOpenApi …
Run Code Online (Sandbox Code Playgroud)

java spring-boot openapi springdoc

5
推荐指数
2
解决办法
3416
查看次数

升级到 Angular 11 后,必须从注入上下文中调用“inject()”

升级到 Angular 11 后,我无法再访问ng serve我的 Web 应用程序。

我正在使用 Spring Doc 和最新的 OpenAPI 生成器 gradle-plutin (5.0.0) 生成客户端。

该问题似乎与我的(生成的)REST 客户端有关。打开 https://localhost:4200 会将以下内容写入控制台:

main.ts:12 Error: inject() must be called from an injection context
    at injectInjectorOnly (core.js:4901) [angular]
    at Module.??inject (core.js:4911) [angular]
    at Object.ApiModule_Factory [as factory] (meditation-rest-client.js:2885) [angular]
    at R3Injector.hydrate (core.js:11158) [angular]
    at R3Injector.get (core.js:10979) [angular]
    at :4200/vendor.js:82591:55 [angular]
    at Set.forEach (<anonymous>) [angular]
    at R3Injector._resolveInjectorDefTypes (core.js:11016) [angular]
    at new NgModuleRef$1 (core.js:25046) [angular]
    at NgModuleFactory$1.create (core.js:25100) [angular]
    at :4200/vendor.js:100468:45 [angular]
    at Object.onInvoke (core.js:28301) …
Run Code Online (Sandbox Code Playgroud)

openapi angular openapi-generator springdoc

5
推荐指数
2
解决办法
1570
查看次数

Swagger/openapi 将 @Schema(description) 添加到每个枚举值

我正在使用 Swagger/OpenApi V3 注释创建应用程序的 API 描述,从以下依赖项导入:

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.5.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我想将描述添加到每个枚举值:

public enum Gender {

    @Schema(description = "NotSpecified")
    NOT_SPECIFIED,

    @Schema(description = "Male")
    MALE,

    @Schema(description = "Female")
    FEMALE;
}
Run Code Online (Sandbox Code Playgroud)

但 springdoc 不会生成 enum 的描述。

Gender:
  type: string
  example: MALE
  enum:
  - NOT_SPECIFIED
  - MALE
  - FEMALE
Run Code Online (Sandbox Code Playgroud)

有没有办法为每个枚举值添加描述?

java swagger openapi springdoc

5
推荐指数
1
解决办法
3945
查看次数

如何注释 DTO 以使其显示在 SwaggerUI 架构中?

我有一个带有@RequestBodyDTO 的控制器。我需要显示 DTO 的架构,而不是stringSwagger 中 RequestBody 架构中的默认架构。

通过在 API 之上使用 @Operation 并在其中使用 @Parameter,我已经能够在两个地方描述 DTO

图片描述

并填写示例(参见代码)。我已经尝试@Schema@Operation(在 requestBody 下)和@Parameter注释。前者抛出 NPE,后者不做任何改变,并针对 DTO 本身中的对应注释进行了各种尝试。

样品控制器

@RequestMapping(value = "/{myPathVar}", method = RequestMethod.POST)
@Operation(summary = "Create something.", 
    parameters = { @Parameter(in = ParameterIn.PATH, name = "myPathVar", description = "Some path variable. Swagger uses this description.") },             
    requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
        description = "My description here.", 
        content = @Content(examples = @ExampleObject("{\"A\" : \"a\",\"B\" : \"{\"b\" : \"foo\", \"bb\" …
Run Code Online (Sandbox Code Playgroud)

swagger spring-boot springdoc springdoc-openapi-ui

5
推荐指数
1
解决办法
2万
查看次数

如何更改生成配置的 swagger-ui 端点 (/v3/api-docs/swagger-config)

我有一个创建 swagger-ui 端点的服务。我希望此端点托管在与默认路径不同的路径上(由于 Kubernetes 入口规则)。这可以通过使用 springdoc swagger 轻松实现

springdoc.swagger-ui.path=/myPath/swagger-ui/index.html
Run Code Online (Sandbox Code Playgroud)

但是它正在尝试从默认 url 访问配置

/v3/api-docs/swagger-config
Run Code Online (Sandbox Code Playgroud)

不幸的是我还需要这个网址位于特定路径上。我知道有一个设置可以指定查找 swagger-config 的路径,即:

springdoc.swagger-ui.configUrl=/myPath/v3/api-docs/swagger-config
Run Code Online (Sandbox Code Playgroud)

但这不是我要找的。此设置允许您指定不同的配置源,然后您需要在指定路径上创建资源,否则将找不到该资源。如果我理解正确,默认路径 /v3/api-docs/swagger-config 是某种端点,它会自动创建/生成资源,而不需要用户创建它。

我正在寻找的是一种在不同路径上访问此自动生成的配置的方法。会说“如果我访问 /myPath/v3/api-docs/swagger-config 而不是 /v3/api-docs/swagger-config,则生成并返回您的默认配置”。最好通过 application.properties 中的条目或覆盖应用程序代码中的某些行为

有谁知道如何实现这一点?

java swagger swagger-ui spring-boot springdoc

5
推荐指数
2
解决办法
5860
查看次数

升级到 Spring Boot 2.6 时 Spring Security 和 org.springdoc.ui.SwaggerConfig 之间的循环引用

将我的应用程序从 Spring Boot 2.5 升级到 2.6 时,出现此错误:

创建名称为“securityConfig”的 bean 时出错:通过方法“setContentNegotationStrategy”参数 0 表达的依赖关系不满足;

嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration”的 bean 时出错:通过方法“setConfigurers”参数 0 表示不满足的依赖关系;

嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“org.springdoc.ui.SwaggerConfig”的bean时出错:通过字段“swaggerIndexTransformer”表达的依赖关系不满足;

嵌套异常是org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名称为“org.springdoc.ui.SwaggerConfig”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用?

此错误记录在https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.6-Release-Notes#circular-references-prohibited-by-default上,我知道我可以“修复”通过设置一个属性并返回到 Spring Boot 2.5 行为来实现它。但是如果我可以借此机会修复循环引用,我也可以在将来这样做。

我的securityConfig很简单,因为我的应用程序是一个公共应用程序,向所有调用者返回一些 html 内容,无需身份验证。这是我的配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

  @Override
  protected void configure(HttpSecurity security) throws Exception {
    //application is open to everyone - no security
    security.httpBasic().disable();
  }
}
Run Code Online (Sandbox Code Playgroud)

setContentNegotationStrategy错误提到的方法是我WebSecurityConfigurerAdapter没有覆盖的方法,所以我无法理解我需要做什么。

如果我删除我的SecurityConfig课程,那么我仍然有一个错误,与以前相同,但没有提及我的SecurityConfig课程:

创建名称为“org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration$EnableWebMvcConfiguration”的bean时出错:通过方法“setConfigurers”参数0表示不满足的依赖关系;

嵌套异常是org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“org.springdoc.ui.SwaggerConfig”的bean时出错:通过字段“swaggerIndexTransformer”表达的依赖关系不满足;

嵌套异常是org.springframework.beans.factory.BeanCurrentlyInCreationException:创建名称为“org.springdoc.ui.SwaggerConfig”的bean时出错:请求的bean当前正在创建中:是否存在无法解析的循环引用?

处理 Spring Security 和 org.springdoc.ui.SwaggerConfig 之间的循环引用的推荐方法是什么?

java spring-security spring-boot springdoc

5
推荐指数
1
解决办法
6816
查看次数

找不到 SwaggerWelcomeCommon

尝试将 Springdoc 添加到 spring-boot-2.6.2 项目中。应用程序运行在嵌入式码头服务器上。使用下面的 pom.xml 设置,执行器可以正常运行。

当我尝试运行该应用程序时,出现以下错误。由于我认为这是由于其中一个依赖项而发生的,因此我尝试组织依赖项。

Description:

Parameter 3 of method indexPageTransformer in org.springdoc.webmvc.ui.SwaggerConfig required a bean of type 'org.springdoc.webmvc.ui.SwaggerWelcomeCommon' that could not be found.


Action:

Consider defining a bean of type 'org.springdoc.webmvc.ui.SwaggerWelcomeCommon' in your configuration.
Run Code Online (Sandbox Code Playgroud)

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>demo</groupId>
        <artifactId>demo</artifactId>
        <version>demo-1.01</version>
    </parent>

    <artifactId>demo</artifactId>
    <packaging>war</packaging>

    <properties>
        <package.plantbase>dev</package.plantbase>
        <ojdbc6.version>11.2.0.4</ojdbc6.version>
        <jetty.version>9.4.43.v20210629</jetty.version>
        <jjwt.version>0.9.1</jjwt.version>
        <reflection.version>0.9.11</reflection.version>
        <lombok.version>1.18.4</lombok.version>
        <plexus.version>2.5.2</plexus.version>
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
        </dependency> …
Run Code Online (Sandbox Code Playgroud)

jetty swagger spring-boot springdoc

4
推荐指数
1
解决办法
6644
查看次数