更详细的Swagger的教程可以看:https://www.bilibili.com/video/BV1Y441197Lw?from=search&seid=14534666160936817499&spm_id_from=333.337.0.0
- 引入相关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){
//设置要显示seagger的环境
Profiles dev = Profiles.of("dev");
// 判断当前是否处于dev环境,如果通过显示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(
"佛祖组校园人事系统",
"相关API接口",
"v1",
"urn:tos",
contact,
"Apache2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}
运行项目,在端口号后 输入:/swagger-ui.html 访问接口信息