Interface documents play an indispensable role in our daily development work. Especially for projects with separate front and back ends, interface documents need to be used for communication. Swagger 2 is open source and free to use. Tool of
1. Introduce Swagger2 dependency
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2. Swagger2 configuration file SwaggerConfig.java
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.pathMapping("/")
.select()
// 扫描路径
.apis(RequestHandlerSelectors.basePackage("com.meet.controller"))
.paths(PathSelectors.any())
.build().apiInfo(new ApiInfoBuilder()
// 标题
.title("Swagger项目管理")
// 描述
.description("Swagger项目管理接口测试文档")
// 版本号
.version("1.0.0")
// 联系人信息
.contact(new Contact("lizhou","http://127.0.0.1:8080/swagger-ui.html","[email protected]"))
// 使用协议
.license("The Apache License")
.licenseUrl("https://swagger.io/")
.build());
}
}
We first use the @Configuration annotation to indicate that this is a configuration class,
then use the @ EnableSwagger2 annotation to open Swagger2
and then configure a Docket Bean . In this Bean, configure the path of the scan package, which is the path of the Controller class . The main configuration website information in
ApiInfo , Such as title, description, contact, usage agreement and other information
3. Get started
To start the project, visit:
http://localhost:8080/swagger-ui.html
Here you can see all the scanned API interfaces
4. Expand knowledge
@Api () is used for a class, indicating that this class is a resource of swagger
@ApiOperation () is used for a method, indicating an operation of an http request
@ApiParam () is used for a method, parameter, field description, indicating the addition of metadata to the parameter (Description or required, etc.)
@ApiModel () is used for classes, which means to explain the class, for parameters to be received by the entity class
@ApiModelProperty () for methods, fields, which means that the description of the model attribute or data operation changes
@ ApiIgnore () is used for classes, methods, method parameters, indicating that this method or class is ignored
@ApiImplicitParam () is used for methods, indicating a separate request parameter
@ApiImplicitParams () is used for methods, contains multiple @ApiImplicitParam
E.g:
1. @Api () annotation
@Controller
@Api(tags = "添加好友相关接口")
@RequestMapping("addFriend")
public class AddFriendController {
...
}
2. @ApiOperation () Note
If you find any deficiencies in reading, please leave a message! ! !