一,引入Swagger3依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
这里不使用Swagger2的依赖了,使用Swagger2的依赖,需要两个dependency。
还要配置相应的propertis,可参考:https://blog.csdn.net/baidu_41909837/article/details/121573831
二,在启动类加EnableWebMvc
三,添加SwaggerConfig,添加配置类。
每部分的作用在下面代码注释中。
package com.seckill.seckilldemo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2) //指定Api类型为Swagger2
.apiInfo(apiInfo()) //指定文档汇总信息
.select()
.apis(RequestHandlerSelectors
.basePackage("com.seckill.seckilldemo.controller")
) //指定controller包路径
.paths(PathSelectors.any()) //指定展示所有controller
.build();
}
private ApiInfo apiInfo(){
//返回一个apiinfo
return new ApiInfoBuilder()
.title("Api接口文档") //文档页标题
.contact(
new Contact(
"Jackwong",
"https://blog.csdn.net",
"2537617411@qq.com")
) // 联系人信息,姓名,邮箱。
.description("api文档") // 详细信息
.version("1.0.1") // 文档版本号
.termsOfServiceUrl("https://www.baidu.com") //网站地址
.build();
}
}
测试:
1,写一个Controller:
2,启动Springboot启动类。
3,访问http://localhost:8080/swagger-ui/index.html