【问题标题】:Generate swagger docus from java Annotations, missing security definitions从 java Annotations 生成 swagger 文档,缺少安全定义
【发布时间】:2017-08-06 13:11:30
【问题描述】:

我使用 swagger maven 插件在构建时生成 swagger 文档。

这适用于基本的@SwaggerDefinition 注释。 但子部分securityDefinition在最终的json和yaml文件中并没有生成。

我在 3.1.4 版本中使用 swagger-maven-plugin

任何想法可能缺少什么?

@SwaggerDefinition(
        info = @Info(
                description = "Interact with example",
                version = "V1.1.0",
                title = "The example API",
                termsOfService = "http://example.com",
                contact = @Contact(
                   name = "André Schild", 
                   email = "a.schild@aarboard.ch", 
                   url = "http://example.com"
                ),
                license = @License(
                    name = "example License",
                    url = "http://example.com/"
                )
        ),
        host = "api.example.com",
        basePath = "/api/v1",
        consumes = {"application/json"},
        produces = {"application/json"},
        schemes = {SwaggerDefinition.Scheme.HTTPS},
        securityDefinition = @SecurityDefinition(
                basicAuthDefinions = {
                        @BasicAuthDefinition(key = "basicAuth")},
                apiKeyAuthDefintions = {
                        @ApiKeyAuthDefinition(key = "exampleAuth", name = "apiKey", in = ApiKeyLocation.HEADER)}),
          tags = {
                @Tag(name = "API", description = "Api for example"),
                @Tag(name = "V1", description = "V1 Api for example")
        }, 
        externalDocs = @ExternalDocs(
                value = "example",
                url = "http://example.com"
        )
)

最终的 swagger 文件如下所示:

---
swagger: "2.0"
info:
  description: "Interact with example"
  version: "V1.1.0"
  title: "The example API"
  termsOfService: "http://example.com"
  contact:
    name: "André Schild"
    url: "http://example.com"
    email: "a.schild@aarboard.ch"
  license:
    name: "example License"
    url: "http://example.com/"
host: "api.example.com"
basePath: "/api/v1"
tags:
- name: "categories"
  description: "Operations about categories"
paths:
  /categories:
    get: 
.... and more paths/definitions....

【问题讨论】:

    标签: java maven annotations swagger


    【解决方案1】:

    我得到这个为我工作的方法是创建一个单独的类/接口并用@SwaggerDefinition 注释它和安全定义。它为所有 API 提供了安全定义。

    像这样:

    @SwaggerDefinition(securityDefinition = @SecurityDefinition(apiKeyAuthDefinitions = { @ApiKeyAuthDefinition(key = "ApiKey", name = "Authorization", in = ApiKeyLocation.HEADER) }))
    public interface SwaggerSecurityDefinition {
    
    }
    

    【讨论】:

    • 嗯.. 这对我不起作用。我已经在与其他定义相同的位置创建了新的接口类,但它没有包含在文档中...
    【解决方案2】:

    @安德烈:

    要使 ritesh 的解决方案正常工作,您需要在资源类中包含安全定义:

    @Api(value = "/example", description = "", authorizations = {
            @Authorization(
                    value = "ApiKey"
            )
    })
    public class ExapleResource {
    ...
    }

    或者您可以使用 swagger bean 来全局配置定义。 在swagger-examples

    中找到了一个很好的例子

    【讨论】:

      【解决方案3】:

      如果使用它,请配置 swagger-maven-plugin (https://github.com/kongchen/swagger-maven-plugin):

      <securityDefinition>
          <name>MybasicAuth</name>
          <type>basic</type>
      </securityDefinition>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-11-25
        • 2016-12-11
        • 2012-02-01
        • 2019-05-03
        • 1970-01-01
        • 2018-06-11
        • 2016-09-14
        • 2016-03-24
        相关资源
        最近更新 更多