2017-01-23 5 views
8

私は自分のプロジェクト用のREST APIを作成するためにjava springブートフレームワークを使用しています。私は驚くべきドキュメントを生成するために "springfox-swagger2 and springfox-swagger-ui"を使用しています。私はURL http://localhost:8080/swagger-ui.htmlを使って私の文書を見ることができます。swagger.jsonの生成方法

にはどうすればクレートまたは生成swagger.json/spec.jsonは、ドキュメントには、あなたがしてURLを取得することができ、我々はAPIドキュメント

答えて

0

私はこれが正しい方法であるかを知りませんしかし、それは:)

依存関係を働いていない

<dependency> 
     <groupId>io.springfox</groupId> 
     <artifactId>springfox-swagger2</artifactId> 
     <version>2.4.0</version> 
    </dependency> 

    <dependency> 
     <groupId>io.springfox</groupId> 
     <artifactId>springfox-swagger-ui</artifactId> 
     <version>2.6.1</version> 
    </dependency> 
5

を一覧表示するために別のアプリケーションを使用しているこのアプリケーションであってはならないことができます威張っ-UI htmlページ:

enter image description here

GET http://localhost:8080/v2/api-docs?group=App 

そして、実際にあなたがクローム/ FirefoxですべてのURLは、ツールのネットワーク機能を開発し得ることができます。

+1

方法D oこのURLからswagger.json/spec.jsonファイルをダウンロードしますか? –

+0

ブラウザで完全なURLを入力し、JSONをレスポンスとして取得します。どのファイルをjsonファイルとしてカットアンドペーストすることができますか? –

0

あなたはMavenを使用する場合は、あなたがあなたのpom.xmlにこれを追加swagger-maven-plugin

を使用して、クライアント側とサーバー側のドキュメント(YAML、JSONやHTML)を生成することができます。

..... 
<plugin> 
       <groupId>com.github.kongchen</groupId> 
       <artifactId>swagger-maven-plugin</artifactId> 
       <version>3.0.1</version> 
       <configuration> 
        <apiSources> 
         <apiSource> 
          <springmvc>true</springmvc> 
          <locations>com.yourcontrollers.package.v1</locations> 
          <schemes>http,https</schemes> 
          <host>localhost:8080</host> 
          <basePath>/api-doc</basePath> 
          <info> 
           <title>Your API name</title> 
           <version>v1</version> 
           <description> description of your API</description> 
           <termsOfService> 
            http://www.yourterms.com 
           </termsOfService> 
           <contact> 
            <email>[email protected]</email> 
            <name>Your Name</name> 
            <url>http://www.contact-url.com</url> 
           </contact> 
           <license> 
            <url>http://www.licence-url.com</url> 
            <name>Commercial</name> 
           </license> 
          </info> 
          <!-- Support classpath or file absolute path here. 
          1) classpath e.g: "classpath:/markdown.hbs", "classpath:/templates/hello.html" 
          2) file e.g: "${basedir}/src/main/resources/markdown.hbs", 
           "${basedir}/src/main/resources/template/hello.html" --> 
          <templatePath>${basedir}/templates/strapdown.html.hbs</templatePath> 
          <outputPath>${basedir}/generated/document.html</outputPath> 
          <swaggerDirectory>generated/swagger-ui</swaggerDirectory> 
          <securityDefinitions> 
           <securityDefinition> 
            <name>basicAuth</name> 
            <type>basic</type> 
           </securityDefinition> 
          </securityDefinitions> 
         </apiSource> 
        </apiSources> 
       </configuration> 
      </plugin> ........ 
ダウンロードでき

を*このアドレスの.hbsテンプレート: https://github.com/kongchen/swagger-maven-example

がMVN闊歩を実行します。 JSONドキュメントを生成するには、プロジェクト/生成/闊歩/ディレクトリに生成されます。このアドレスにそれ過去 : http://editor.swagger.io

そして、あなたが欲しい、これまで何を生成する(お好みの技術でサーバー側またはクライアント側API)

2

私は遅く、ここで少しだけど、私はちょうどあなたのことを考え出しましたブラウザのコンソールを開き、SwaggerドキュメントのJSON定義を返すGETリクエストのURLを見つけることができます。 APIをAWS API Gatewayにマッピングする際には、以下の手法が役に立ちました。これを行うために

:あなた闊歩ドキュメントに

  1. ナビゲート
  2. オープンエンドポイントブラウザのコンソール
  3. はXHRすることで、ネットワークタブとフィルタにページ
  4. 移動をリフレッシュ
  5. 右を要求?format=openapi
  6. XHRリクエストをクリックするだけで、新しいJSONにコピーして貼り付けることができますファイル!私は私のホームコントローラのテストケース

    import org.springframework.boot.test.web.client.TestRestTemplate; 
    
    public class HomeControllerTest extends .... ...... { 
    
    @Autowired 
    private TestRestTemplate restTemplate; 
    
    
    @Test 
    public void testHome() throws Exception { 
        //....... 
        //... my home controller test code 
        //..... 
    
        String swagger = this.restTemplate.getForObject("/v2/api-docs", String.class); 
    
        this.writeFile("spec.json", swagger); 
    } 
    
    public void writeFile(String fileName, String content) { 
    
        File theDir = new File("swagger"); 
    
        if (!theDir.exists()) { 
         try{ 
          theDir.mkdir(); 
         } 
         catch(SecurityException se){ }   
        } 
    
        BufferedWriter bw = null; 
        FileWriter fw = null; 
        try { 
         fw = new FileWriter("swagger/"+fileName); 
         bw = new BufferedWriter(fw); 
         bw.write(content); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } finally { 
         try { 
          if (bw != null) 
           bw.close(); 
          if (fw != null) 
           fw.close(); 
         } catch (IOException ex) { 
          ex.printStackTrace(); 
         } 
    
        } 
    
    } 
    } 
    

    の末尾に次のコードを追加した小さなトリック

    でこれを行っている

+0

+ JSONが利用可能なXHRリクエストのレスポンスボディコンテンツをコピーする必要があります – Vijai

関連する問題