簡介
Swagger2是我在SpringCloud開發過程中用的最多的接口測試工具。
Swagger2是一個規範和完整的框架,用於生成、描述、調用和可視化Restful風格的web服務。
POM文件
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
啓動類
需要引入註解@EnableSwagger2(如果config文件存在則在config中引入)
config文件
package com.qy.test.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
//@Profile({"dev", "test"})//在生產環境不開啓
public class Swagger2Config implements WebMvcConfigurer {
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInf())
.select()
.apis(RequestHandlerSelectors.basePackage("com.qy.test.demo.controller"))//掃描位置
.paths(PathSelectors.any())
.build();
}
private ApiInfo buildApiInf() {
return new ApiInfoBuilder()
.title("spring測試")
.description("第一次在個人電腦搭建項目")
.contact(new Contact("帝風科技", "https://blog.csdn.net/qq_36841482", "[email protected]"))
.version("1.0")
.build();
}
}
訪問地址:http://127.0.0.1:8001/swagger-ui.html(項目路徑加後綴)
展示情況如下,對照上放個字段含義。