Swagger Gradle插件在构建期间未正确为spring mvc生成openapi.json文件

时间:2018-10-18 20:27:51

标签: java spring spring-boot gradle swagger

我想导入使用swagger gradle插件创建的API定义文件。我有一个Spring Boot MVC应用程序。我似乎无法正确获得解析设置以生成openAPI.json。 gradle resolve任务仅使用“ openapi”:“ 3.0.1”标记和值创建一个空文件。解决任务是不使用swagger和mvc api批注。您能为我指出正确的方向以找到有关swagger gradle resolve任务的正确配置设置吗?

当应用程序运行并且我查看swagger-ui端点时,该应用程序显示Spring-UI文档没有问题。问题是在gradle构建期间,有一个单独的gradle任务来生成openAPI.json文件,我希望将其导入到SwaggerHub中,作为应用程序API目录的一部分。

我一直找不到任何有关如何配置swagger gradle插件解决任务以获取Spring MVC API注释的文档。我可以使它在使用jax-rs批注的另一个应用程序上正常工作,因此可以确定这是一个解决任务配置问题。

我的gradle.build文件的副本:

buildscript {
    ext {
        springBootVersion = '1.4.1.RELEASE'
    }
    repositories {
        mavenCentral()
        maven {
          url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
        classpath("io.spring.gradle:dependency-management-plugin:0.5.6.RELEASE")

        // SwaggerHub Plugin Dependency
        classpath("gradle.plugin.io.swagger:swaggerhub:1.0.1")

        // Swagger Gradle Plugin Dependency
        classpath("io.swagger.core.v3:swagger-gradle-plugin:2.0.5") 

    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'
apply plugin: 'jacoco'
// Required to upload swagger api doc to swaggerhub
apply plugin: 'io.swagger.swaggerhub'
// Required to generate swagger api doc file to upload to swaggerhub
apply plugin: 'io.swagger.core.v3.swagger-gradle-plugin'


jar {
    baseName = 'service-framework'
    version = '0.0.1-SNAPSHOT'
}

javadoc {
    source = sourceSets.main.allJava
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-aop')
    compile('org.springframework.boot:spring-boot-starter-jersey')
    compile('org.springframework.boot:spring-boot-starter-web')

    // Swagger Dependencies to generate the api documentation
    compile('io.swagger:swagger-annotations:1.5.20')
    compile('io.springfox:springfox-swagger2:2.8.0')
    compile('io.springfox:springfox-swagger-ui:2.8.0')

    runtime('com.h2database:h2')
    runtime('org.postgresql:postgresql')
    testCompile('org.springframework.boot:spring-boot-starter-test')
    testCompile group: 'org.hamcrest', name: 'hamcrest-core', version: '1.3'
    testCompile group: 'junit', name: 'junit-dep', version: '4.10'
    testCompile group: 'org.hamcrest', name: 'hamcrest-library', version: '1.3'
}

// Command to the swagger gradle plugin to generate the api file for upload
// to swaggerhub
//
//
// Create the directory for output of the api document
File path = new File("${buildDir}/doc")
//
// The Swagger Core Task to create the json output file required to upload
// to SwaggerHub
//
resolve {
    outputFileName = 'openAPI'
    outputFormat = 'JSON'
    prettyPrint = 'TRUE'
    classpath = sourceSets.main.runtimeClasspath
    resourcePackages = ['test.serviceframework']
    outputPath = path.getAbsolutePath();
}

openAPI.json文件中的结果输出:

{
  "openapi" : "3.0.1"
}

1 个答案:

答案 0 :(得分:0)

似乎还没有针对Spring(MVC / Rest)和OpenAPI 3的自动文档编制工具。

SpringFox对Swagger 2进行了此操作。看来您已经添加了此功能,因此您应该在/ v2 / api-docs

上看到Swagger 2 Doc。

io.swagger.core为JAX-RS和Jersey提供OpenAPI 3

此时的选项是:

  • 生成Swagger 2,并将其转换为OpenAPI 3
  • 在Spring内运行JAX-RS