更詳細(xì)的Swagger的教程可以看:https://www.bilibili.com/video/BV1Y441197Lw?from=search&seid=14534666160936817499&spm_id_from=333.337.0.0
- 引入相關(guān)swagger2依賴包
<!--swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--swagger-ui-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2.自定義SwaggerConfig類
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
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;
import java.util.ArrayList;
@Configuration //開啟swagger配置
@EnableSwagger2 //開啟swagger2
public class SwaggerConfig {
@Bean
public Docket docket(Environment environment){
//設(shè)置要顯示seagger的環(huán)境
Profiles dev = Profiles.of("dev");
// 判斷當(dāng)前是否處于dev環(huán)境,如果通過顯示API信息酥泞,否則不顯示
boolean flag = environment.acceptsProfiles(dev);
return new Docket(DocumentationType.SWAGGER_2)
.groupName("佛祖") //分組
.enable(flag) //是否開啟swagger
.select() //掃描
.apis(RequestHandlerSelectors.basePackage("com.littlenorth.controller")) //掃描此包下面的Api
.build();
}
//swagger接口文檔信息
private ApiInfo apiInfo(){
Contact contact=new Contact("佛祖組","","1835857596@qq.com");
return new ApiInfo(
"佛祖組校園人事系統(tǒng)",
"相關(guān)API接口",
"v1",
"urn:tos",
contact,
"Apache2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
運(yùn)行項(xiàng)目,在端口號后 輸入:/swagger-ui.html 訪問接口信息