SpringBoot下swagger3.0的配置

1、swagger3.0依赖

<dependency>
    <groupId>io.springfox</groupId>
     <artifactId>springfox-swagger2</artifactId>
     <version>3.0.0</version>
</dependency>

我的springboot版本

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.12</version>
        <relativePath/>
    </parent>

2、swagger配置类

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;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zystart.sys.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Quartz定时任务")
                .description("测试swagger配置定时任务")
                //       .termsOfServiceUrl("https://www.baidu.com/")
                .version("1.0")
                .build();
    }

}

3、我的application.yml配置

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

4、访问地址

http://localhost:port/swagger-ui/index.html

5、Swagger注解说明

注解说明
@Api修饰整个类,描述Controller的作用
@ApiOperation描述一个类的一个方法,或者说一个接口
@ApiParam单个参数描述
@ApiModel用对象来接收参数
@ApiProperty用对象接收参数时,描述对象的一个字段

参考文章:
1、 SpringBoot集成swagger-ui以及swagger分组显示
2、IDEA报错之Failed to start bean ‘documentationPluginsBootstrapper‘问题及解决方案
3、swagger3.0访问后台地址
4、集成Springfox 3.0.0

### SpringBoot集成Swagger3.0访问路径404解决方案 在Spring Boot项目中集成Swagger3.0时,如果遇到访问路径返回404错误的情况,可能是由于以下几个原因引起的: #### 1. **版本兼容性问题** 从Spring Boot 2.6.0开始,默认使用`PathPatternParser`作为路径匹配器,而Swagger的旧版依赖仍然基于`AntPathMatcher`。这种不一致可能导致某些路径无法正常解析[^2]。 解决方法是在项目的`application.yml`文件中禁用新的路径模式解析器并恢复到传统的`AntPathMatcher`方式: ```yaml spring: mvc: pathmatch: matching-strategy: ant_path_matcher ``` #### 2. **引入正确的依赖** 确保项目中的Maven或Gradle配置包含了最新的Swagger支持库。对于Swagger3.0及以上版本,推荐使用以下依赖项[^1][^3]: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 注意:如果仅引入上述依赖仍无法解决问题,则可能需要额外添加Knife4j插件来增强功能和支持更复杂的场景[^4]。 #### 3. **正确设置启动类上的注解** 根据所使用的Swagger具体版本调整应用主程序入口处的相关注解定义: - 对于低于3.0.0版本应标注为`@EnableSwagger2`; - 而等于或者高于此界限则替换成为`@EnableOpenApi`. 例如,在SpringBootApplication上加上如下声明即可完成初始化工作: ```java import springfox.documentation.swagger2.annotations.EnableSwagger2; @SpringBootApplication @EnableOpenApi // For Swagger 3.x versions. public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } } ``` #### 4. **确认资源映射地址无误** 随着Swagger迭代升级其默认UI页面位置也有所改变: - 版本号小于3.0.0时通过浏览器打开链接形如 `http://localhost:<port>/swagger-ui.html`. - 大于等于该临界点后需改访另一形式即 `http://localhost:<port>/swagger-ui/index.html#/` 或者简化后的 `/swagger-ui/`. 因此当发现请求失败提示找不到目标文件夹时候可以先核实当前安装包实际对应哪一类情况再做相应修改尝试重新加载网页查看效果如何. #### 示例代码片段展示完整流程 下面给出一段综合性的实现样例供参考学习之用: ```java @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值