springfox-swagger-ui provides static Javascript UI for visualizing the Swagger schema definitions.
Add a @Configuration class to enable Swagger.
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket postsApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("public-api")
.apiInfo(apiInfo())
.select()
.paths(postPaths())
.build();
}
private Predicate<String> postPaths() {
return or(
regex("/api/posts.*"),
regex("/api/comments.*")
);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("SpringMVC Example API")
.description("SpringMVC Example API reference for developers")
.termsOfServiceUrl("http://hantsy.blogspot.com")
.contact("Hantsy Bai")
.license("Apache License Version 2.0")
.licenseUrl("https://github.com/springfox/springfox/blob/master/LICENSE")
.version("2.0")
.build();
}
}
When the application starts up, it will scan all Controllers and generate Swagger schema definition at runtime, Swagger UI will read definitions and render user friendly UI for REST APIs.
You can save this page content as a json file and upload to http://editor.swagger.io and edit it online.
The Swagger schema definition generation will consume lots of system resourcs at runtime.
Combined with Springfox, Swagger2Markup project, and Spring RestDocs, the Swagger schema definition can be converted to asciidocs, and with asciidoctor-maven-plugin, the asciidocs can be generated into static HTML5 or PDF files.
Add swagger2-markup-maven-plugin into pom.xml file.