Использование Javadocs для создания документа Swagger
Я хочу создать документацию Swagger для существующего набора API RESTful. У меня есть следующее требование:
- Создать Swagger Doc в автономном режиме (я использовал http://kongchen.github.io/swagger-maven-plugin/). Этот плагин помогает мне генерировать документацию Swagger во время компиляции.
- Чтение существующего Javadoc, чтобы они могли использоваться в документации Swagger.
До сих пор с использованием вышеупомянутого плагина мне удалось достичь точки № 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 делает мою документацию великолепной.
Однако, мой вопрос: могу ли я использовать Javadocs вместо того, чтобы каждый разработчик создавал @ApiOperation и @ApiResponses, чтобы он экономил время для моей команды?
Ответы
Ответ 1
Вы можете создать swagger-ui из Javadoc, используя Enunciate, который имеет модуль Swagger. Во-первых, вам нужно добавить плагин 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 package
, и он будет генерировать файлы документации Swagger из вашего Javadoc.
Ответ 2
Кажется, что javadoc doclet для генерации списка ресурсов JSON Swagger:
https://github.com/teamcarma/swagger-jaxrs-doclet