Ich möchte die Swagger-Dokumentation für einen vorhandenen Satz von RESTful-APIs erstellen. Ich habe folgende Anforderung:
Bisher konnte ich mit dem obigen Plugin Punkt Nr. 1 erreichen. Also für eine bestehende REST -Methode:
/**
* <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 ) {
}
Ich konnte die Swagger-Dokumentation erstellen. Die Verwendung von @ApiOperation & @ApiResponses lässt meine Dokumentation gut aussehen.
Meine Frage ist jedoch: Kann ich die Javadocs verwenden, anstatt jeden Entwickler dazu zu veranlassen, @ApiOperation & @ApiResponses zu erstellen, sodass mein Team Zeit spart?
Sie können Swagger-ui aus Javadoc erstellen, indem Sie Enunciate verwenden, das über ein Swagger-Modul verfügt. Zuerst müssen Sie das Maven-Plugin zu Ihrer POM-Datei hinzufügen. z.B.
<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' enthält Ihre projektspezifischen Konfigurationen und sieht folgendermaßen aus:
<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>
Führen Sie dann mvn compile
aus und es werden Swagger-Dokumentationsdateien aus Ihrem Javadoc generiert.
Es scheint ein Javadoc-Doclet zum Generieren der JSON Swagger-Ressourcenliste zu geben: https://github.com/teamcarma/swagger-jaxrs-doclet