swagger 3.0 与 spring boot 2.7.8 集成
·
使用swagger3集成boot2.7的时候遇到了问题,调试很久才成功,所以记录一下,大家少走弯路。
我的环境是 springboot 2.7.8
1、引入swagger3的依赖
只需要引入这1个即可,其他相关依赖会自动引入。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2、配置 application.yml
spring:
mvc:
pathmatch:
matching-strategy: ANT_PATH_MATCHER
# 启用或关闭swagger
springfox:
documentation:
enabled: true
3、配置 SwaggerConfig
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
public class SwaggerConfig {
Boolean swaggerEnable=true;
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo())
// 是否开启
.enable(swaggerEnable).select()
// 扫描的路径包
.apis(RequestHandlerSelectors.basePackage("xxx.xxx.controller"))
// 指定路径处理PathSelectors.any()代表所有的路径
.paths(PathSelectors.any()).build().pathMapping("/");
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger3集成SpringBoot2.7 - demo示例")
.description("springboot | swagger")
// 作者信息
.contact(new Contact("作者信息", "个人主页url", "email"))
.version("1.0.0")
.build();
}
}
4、添加Controller接口
在需要暴露接口的Controller添加api标记
@Api(value = "商品类别管理", tags = "商品类别管理")
@RestController
public class DemoController {
}
5、访问文档首页
swagger3的默认首页
http://localhost:8080/swagger-ui/index.html
以上就是swagger整合的主要内容,接下来作为参考资料。
无论是后端开发人员或前端开发人员,亦或是测试人员,总是需要与调试项目沟通。前端需要接口数据,但我不知道接口地址。我不能一个一个地发送接口地址。这是一个很好的时间,所以Swagger接管了项目界面的管理。因此,前端只需要这个接口文档来获取数据、数据处理等等。提高开发效率。
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)