从java Annotations生成swagger docus,缺少安全定义

时间:2017-03-15 16:42:43

标签: java maven annotations swagger

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

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

我在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: "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....

3 个答案:

答案 0 :(得分:0)

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

像这样:

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

}

答案 1 :(得分:0)

@Andre:

要获得ritesh的解决方案,您需要在资源类中包含安全性定义:



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




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

中有一个很好的例子

答案 2 :(得分:0)

使用swagger-maven-plugin进行配置(https://github.com/kongchen/swagger-maven-plugin):

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