Open API
OpenApi is the industry’s true API documentation standard, which is maintained by Swagger and listed by Linux as an API standard, thus becoming the industry standard.
Swagger
Swagger is an API documentation maintenance organization that has since become the primary definer of the Open API standard, the latest version of which is Swagger3 (Open API 3) released in 2017. Swagger2 is named IO. Swagger, while swagger3 is named IO. Swagger.core. V3.
This article will teach you how to integrate Swagger3 in a few simple steps.
-
Add Swagger3 dependencies to the project’s POM file
<! --Swagger3--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> The < version > 3.0.0 < / version > < / dependency >Copy the code
-
Enable Swagger3 by adding the @enableOpenAPI annotation to the SpringBoot boot class
@SpringBootApplication @EnableOpenApi public class DemoAllApplication {
public static void main(String[] args) { SpringApplication.run(DemoAllApplication.class, args); } Copy the code
}
-
Add Swagger3 Custom Configuration Class (Optional)
@Configuration public class Swagger3Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); }
Private ApiInfo ApiInfo () {return new ApiInfoBuilder().title("Swagger3 API documentation ").description(" Lu code community "). Contact (new contact (" lu code community ", "http://localhost:8080/swagger-ui/index.html", "[email protected]")). The version (" 1.0 "). The build (); }Copy the code
}
-
Add Swagger annotations to the controller
@API (tags = “controller –Hello”) @requestMapping (“/ Hello”) @RestController Public Class HelloController {
@postmapping ("/insert") @APIOperation (" insert") public String insert() {return "insert"; } @putMapping ("/update") @apiOperation (" update") public String update() {return "update"; } @deletemapping ("/delete") @apiOperation (" delete method --delete") public String delete() {return "delete"; } @getMapping ("/getAll") @apiOperation (" query method --getAll") public String getAll() {return "getAll"; }Copy the code
}
-
Start the project, visit: http://localhost:8080/swagger-ui/index.html
-
Add Bootstrap-UI support, add Swagger-Bootstrap-UI dependency (optional)
<! --swagger-bootstrap-ui--> <dependency> <groupId>com.github.xiaoymin</groupId> < artifactId > swagger - the bootstrap - UI < / artifactId > < version > 1.9.6 < / version > < / dependency >Copy the code
-
Start the project, visit: http://localhost:8080/doc.html (optional)
Related notes:
Tags =" indicates the purpose of the class. You can see the comment "value=" on the UI. This parameter is meaningless, it is also seen on the UI, so no configuration is required. Value =" Notes =" Remarks to the method "@APIIMPLICITParams: Indicates a set of parameters for the requested method. @APIIMPLICITParam: Indicates the parameters for the requested method. Used in the @apiIMPLICITParams annotation to specify aspects of a request parameter Name: parameter name value: Parameter description required: Whether the parameter must be passed paramType: · Header --> retrieve request parameters: @requestheader · query --> retrieve request parameters: @requestParam · path (for restful interfaces) --> retrieve request parameters: @pathVariable · div (not commonly used) · form (not commonly used) dataType: parameter type, default String, other values dataType="Integer" defaultValue: Parameter default @APIresponses: Code: a number, such as 400 message: a message, such as "request parameters not filled in" response: The class that throws the exception @apiModel: Used on response classes to represent a message that returns response data (this is typically used in post creation scenarios such as @requestBody where the request parameters cannot be described using the @APIIMPLICITParam annotation) @APIModelProperty: Used of attributes to describe the attributes of the response classCopy the code
That’s it! Very simple and practical! Hope you enjoy it!
What doubt can comment area to ask! Thumb up! Please forward! For collection!