2016-09-21 3 views
8

swagger-codegenを使用してRESTクライアントと静的HTMLドキュメントを生成したいと考えています。gradleを使ってswagger.jsonを生成するには?

ただし、swagger-codegenにはswagger.jsonが必要です。

私はSwaggerを搭載した実行中のRESTサーバーからこれを得ることができると認識しています。

しかし、Webコンテナでアプリケーションを実行する必要がなく、ブラウザでcurlを指定することなく、Javaコードから直接swagger.jsonを取得する方法があります。それ?

+0

私はまだそれを検討しています。 – tbsalling

+0

https://github.com/gigaSproule/swagger-gradle-plugin このプラグインを試しましたか? あなたが求めていることを正確に行うと主張しています。 –

+0

swagger-gradle-pluginを使用している場合、次のエラーが発生しています。com.fasterxml.jackson.databind.JsonMappingException:[入力:UNKNOWN;で入力終了のためにマップするコンテンツがありません。 ;行:1、列:0] – lex

答えて

1

主なアイデアは、このような何か、buildScriptはGradleの中でそれらを使用できるようにするためにクラスパスに威張っ-mavenの-プラグインやJavaクラスを追加することです:

buildscript { 
    repositories { 
     mavenCentral() 
    } 

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

の最初の行を依存関係は、サブプロジェクトからのスワッガーライブラリを取得し、2行目は、スワッガーアノテーションを含むクラスを取得します。この後

あなたは、単純なJavaクラスとしてのGradleでMavenのプラグインを呼び出すことができます。

// 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: '[email protected]', 
             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あなたはこの例を見つけることができます。

2

要するに、私はとの研究を開始しました...これは少し古いですが、私は正確に同じことを思っていた:

  • 最小限のREST APIを露出させたサンプルの春ブートアプリを。
  • APIメソッドのスワッガー注釈。
  • Springfox;
  • ビルドツールとしてのグラデーション。 swagger-maven-plugin of kongchengradle portを使用することにより

は、私は2つの異なるアプローチを使用してビルドアーティファクトとしてJSONの仕様を生成するために管理しました。

  • (このカウントになっているかどうかはわかりません。なぜなら、サーバーを起動するためです)仕様を生成する統合テスト(Springの模擬MVC)を実行することによってです。私はhereからアイデアを借りました。
  • 私は、hereという簡単なプロジェクトで私の研究をまとめました。 Automationセクションを参照してください。コードとサンプルが含まれています。

    関連する問題