When used in combination with the Spring framework, it automatically generates API documents for the target system.
In addition, it is possible to suppress ** human mistakes ** and make documents with ** uniformity ** by ** automatically generating ** from the system.
There are many points, so I'll list them ... φ (...)
Managed by a definition file called Swagger Spec
Defined in JSON or YAML
Swagger UI documents based on Swagger Spec
Generated documents can be customized based on HTML and jQuery
Supports frameworks in various languages
It is also possible to create like r using Swagger core without creating Swagger Spec
There are other tools such as Swagger Editor, Swagger Codegen, and Swagger Node (apparently).
External tools such as Postman and Amazon API Gateway are also supported.
Swagger Test Template automatically generates and executes JavaScript test code from Spec files.
Swagger is adopted by the "Open API Initiative", an organization that promotes standard formats for describing RESTful API interfaces.
** API blueprint ** is famous as another API document generation tool.
Add to gradle dependency
build.gradle
dependencies {
...
compile "io.springfox:springfox-swagger2:2.2.2"
compile "io.springfox:springfox-swagger-ui:2.2.2"
compile "com.google.guava:guava:17.0"
...
}
Add and enable annotations
Application.java
@SpringBootApplication
@EnableSwagger2//add to
public class Application extends SpringBootServletInitializer
{
public static void main( String... args )
{
SpringApplication.run( Application.class, args );
}
}
SwaggerConfiguration.java
@Configuration
public class SwaggerConfiguration
{
@Bean
public Docket publicDocument()
{
return new Docket( DocumentationType.SWAGGER_2 ).groupName( "public" )
.select()
.paths( paths() )
.build()
.apiInfo( apiInfo() );
}
private ApiInfo apiInfo()
{
ApiInfo apiInfo = new ApiInfo(
"My REST API",
"Some custom description of API.",
"API TOS",
"Terms of service",
"[email protected]",
"License of API",
"API license URL");
return apiInfo;
}
private Predicate<String> paths() {
return or(
regex("/some/endpoint")
);
}
}
Access /swagger-ui.html
It is convenient because the Java class of the return value is automatically converted to JSON SCHEMA and you can make a request from this screen.
next
--How to add header parameters --Pull-down using DB value --Static documentation
I will touch it around.
Well then.
Recommended Posts