Swagger2或者3的实现
参考文章:https://blog.csdn.net/feiying0canglang/article/details/120753090
1.导入相应依赖
<!-- swagger-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
2.对应创建配置文件
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Swagger2的接口配置
*
* @author ruoyi
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig
{
/** 是否开启swagger */
@Value("${swagger.enabled}")
private boolean enabled;
/** 设置请求的统一前缀 */
@Value("${swagger.pathMapping}")
private String pathMapping;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//是否开启swagger
.enable(enabled)
.select()
//扫描的路径使用@Api的controller
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
//指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any())
.build()
//请求URL都会加上/project前缀
.pathMapping(pathMapping);
}
//配置在线文档的基本信息
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("小兔鲜接口文档")
.description("小兔鲜接口文档")
.termsOfServiceUrl("http://localhost:20008")
.version("1.0")
.build();
}
需要修改的地方,请自行修改
3.yml中的配置
# Swagger配置
swagger:
# 是否开启swagger,生产环境关闭
enabled: true
# 请求前缀
pathMapping:
访问地址:ip+端口+pathMapping+/doc.html
如果想简单使用3的版本
4.改变依赖
之前2的依赖删除换上
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
其他不变
参考文章:https://blog.csdn.net/feiying0canglang/article/details/120753090