23

既存の RESTful API セットの Swagger ドキュメントを作成したいと考えています。次の要件があります。

  1. Swagger Doc をオフラインで生成します (私はhttp://kongchen.github.io/swagger-maven-plugin/を使用しました)。このプラグインは、コンパイル時に Swagger ドキュメントを生成するのに役立ちます。
  2. Swagger ドキュメントで使用できるように、既存の Javadoc を読み取ります。

これまでのところ、上記のプラグインを使用してポイント 1 を達成できました。既存の REST メソッドの場合:

 /**
 * <p>
 * Gets the {@link DisplayPreferenceModel} with the name as provided in the parameter. The preference with the given name defined at the tenant or master level is returned.
 * This API gives us the preference if it is eligible for unauthorized access If it is not eligible it throws an Exception saying Authorization required.
 * </p>
 * @param preferenceName
 *            - The name of the preference.
 * @return {@link DisplayPreferenceModel}
 */
@RequestMapping(method = RequestMethod.GET, value = "/preferences/{preferenceName}")
@ApiOperation(value = "This API gives us the preference if it is eligible for unauthorized access If it is not eligible it throws an Exception saying Authorization required", 
                        notes = "No Notes please", response = DisplayPreferenceModel.class)
@ApiResponses(value = { 
                        @ApiResponse(code = 400, message = "Invalid preferenceName supplied"), 
                        @ApiResponse(code = 404, message = "Display Preference Not Found")
                      }
            )
public DisplayPreferenceModel getDisplayPreference( @PathVariable("preferenceName") final String preferenceName ) {
}

Swagger ドキュメントを生成できました。@ApiOperation と @ApiResponses を使用すると、ドキュメントの見栄えが良くなります。

ただし、私の質問は、チームの時間を節約するために、すべての開発者に @ApiOperation と @ApiResponses を作成させる代わりに、Javadocs を使用できるかということです。

4

2 に答える 2

17

Swagger モジュールを持つEnunciateを使用して、Javadoc から swagger-ui を生成できます。まず、maven プラグインを pom ファイルに追加する必要があります。例えば

<plugin>
        <groupId>com.webcohesion.enunciate</groupId>
        <artifactId>enunciate-maven-plugin</artifactId>
        <version>${enunciate.version}</version>
        <executions>
           <execution>
                  <goals>
                    <goal>docs</goal>
                  </goals>
                  <configuration>
                    <configFile>enunciate.xml</configFile>
                    <docsDir>${project.build.directory}</docsDir>
                  </configuration>
           </execution>
        </executions>
</plugin>

ここで、「enunciate.xml」にはプロジェクト固有の構成が含まれており、次のようになります。

<enunciate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="http://enunciate.webcohesion.com/schemas/enunciate-2.0.0-M.3.xsd">

    <application root="/rest" />

</enunciate>

次に実行するmvn compileと、Javadoc から Swagger ドキュメント ファイルが生成されます。

于 2015-11-12T09:00:07.200 に答える
0

JSON Swagger リソース リストを生成するための javadoc doclet があるようです: https://github.com/teamcarma/swagger-jaxrs-doclet

于 2015-09-28T15:58:41.687 に答える