【问题标题】:How to generate swagger.json using gradle?如何使用 gradle 生成 swagger.json?
【发布时间】:2017-01-29 16:20:24
【问题描述】:

我想使用 swagger-codegen 来生成 REST 客户端和可能的静态 HTML 文档。

但是,swagger-codegen 需要 swagger.json 进行输入。

我知道,我可以从配备 Swagger 的正在运行的 REST 服务器获取此信息。

但是有没有办法直接从我的 Java 代码中获取 swagger.json - 即使用源代码中的 gradle 生成它 - 无需在 Web 容器中运行应用程序,并指向 curl 或浏览器给它?

【问题讨论】:

  • 我还在调查。
  • github.com/gigaSproule/swagger-gradle-plugin 你试过这个插件了吗?它声称完全符合您的要求。
  • 在使用 swagger-gradle-plugin 时,我遇到了以下错误:com.fasterxml.jackson.databind.JsonMappingException: No content to map due to end-of-input at [Source: UNKNOWN ;行:1,列:0]

标签: java swagger swagger-codegen


【解决方案1】:

这有点老了,但我想知道完全一样......简而言之,我已经开始研究:

  • 一个示例 Spring Boot 应用程序公开了简约的 REST API;
  • API 方法上的 Swagger 注释;
  • 春狐;
  • Gradle 作为构建工具;

我设法使用两种不同的方法将 JSON 规范生成为构建工件:

  1. 通过使用gradle portswagger-maven-plugin of kongchen
  2. (不确定这是否重要,因为它无论如何都会启动服务器)通过执行生成规范的集成测试(Spring 的模拟 MVC)。我借鉴了here 的想法。

我在一个位于here 的简单项目中总结了我的研究。请参阅Automation 部分。包括代码和示例。

【讨论】:

    【解决方案2】:

    主要思想是将 swagger-maven-plugin 和您的 java 类添加到类路径中,以便 buildScript 能够在 gradle 中使用它们,如下所示:

    buildscript {
        repositories {
            mavenCentral()
        }
    
        dependencies {
            classpath files(project(':swagger-maven-example').configurations['runtime'].files)
            classpath files(project(':swagger-maven-example').sourceSets['main'].output.classesDir)
        }
    }
    

    依赖项中的第一行从子项目获取 swagger 库,第二行获取应该包含 swagger 注释的类。

    在此之后,您可以在 gradle 中调用 maven 插件作为一个简单的 java 类:

    // a trick to have all needed classes in the classpath
    def customClass = new GroovyClassLoader()
    
    buildscript.configurations.classpath.each {
        // println it.toURI().toURL()
        customClass.addURL(it.toURI().toURL())
    }
    
    final ApiDocumentMojo mavenTask = Class.forName('com.github.kongchen.swagger.docgen.mavenplugin.ApiDocumentMojo',true, customClass).newInstance(
            apiSources: [
                    new ApiSource(
                            springmvc: false,
                            locations: ['com/github/kongchen/swagger/sample/wordnik/resource'],
                            schemes: ['http', 'https'],
                            host: 'petstore.swagger.wordnik.com',
                            basePath: '/api',
                            info: new Info(
                                    title: 'Swagger Maven Plugin Sample',
                                    version: 'v1',
                                    description: 'This is a sample for swagger-maven-plugin',
                                    termsOfService: 'http://www.github.com/kongchen/swagger-maven-plugin',
                                    contact: new Contact(
                                            email: 'kongchen@gmail.com',
                                            name: 'Kong Chen',
                                            url: 'http://kongch.com'
                                    ),
                                    license: new License(
                                            url: 'http://www.apache.org/licenses/LICENSE-2.0.html',
                                            name: 'Apache 2.0'
                                    )
                            ),
                            outputPath: file("${buildDir}/swagger/document.html").path,
                            swaggerDirectory: file("${buildDir}/swagger/swagger-ui").path,
                            templatePath: file("${project(':swagger-maven-example').projectDir}/templates/strapdown.html.hbs")
                    )
            ]
    )
    
    // maven plugin
    mavenTask.execute()
    

    Here你可以找到这个例子。

    【讨论】:

    • 它在当前的 Spring Boot 版本中被破坏了。被告知
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-27
    • 2016-06-21
    • 2019-02-09
    • 2018-02-10
    • 2018-04-10
    • 2016-02-19
    相关资源
    最近更新 更多