Swagger 3.0 问题

1,导入环境

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
        <--启动包-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

这个时候springboot会报错!!!


解决方法

解决方式1:把springboot的版本降到2.5-版本的

解决方式2:直接修改配置spring.mvc.pathmatch.matching-strategy=ANT_PATH_MATCHER来更改规则(在properties文件或者yml都可)

原因:这是因为Springfox使用的路径匹配是基于AntPathMatcher的, 而Spring Boot 2.6.X使用的是PathPatternMatcher。


你们以为就完了?不还没有!!!!!


环境配置好了你会发现,当你输入http://localhost:8080/swagger-ui.html会报404,这是因为3.0里的访问地址变了

SwaggerConfig.java 配置


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2//启用swagger
public class SwaggerConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.
                addResourceHandler( "/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
                .resourceChain(false);
    }
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController( "/swagger-ui/")
                .setViewName("forward:" +  "/swagger-ui/index.html");
    }
}

这里是官方文档说明:Springfox Reference Documentation

### 如何配置 Swagger 3.0 API 文档生成 为了在 Spring Boot 应用程序中集成并配置 Swagger 3.0 来自动生成 API 文档,需遵循特定的步骤来设置项目环境。 #### 添加 Maven 或 Gradle 依赖项 首先,在项目的构建文件中添加必要的依赖项。对于 Maven 用户来说,应在 `pom.xml` 文件内加入如下内容: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 而对于使用 Gradle 的开发者,则可以在 `build.gradle` 中添加下面这行代码: ```groovy implementation &#39;io.springfox:springfox-boot-starter:3.0.0&#39; ``` 此操作确保应用程序能够识别和处理 Swagger 相关的功能[^2]。 #### 创建 Swagger 配置类 接着创建一个新的 Java 类用于定义 Swagger 的基本属性和其他选项。此类通常命名为 `SwaggerConfig.java` 并放置于包结构下的合适位置。以下是该类的一个简单实现方式: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } } ``` 上述代码片段展示了如何通过指定控制器的基础包路径以及所要暴露出来的接口路径模式来初始化一个基于 OpenAPI (OAS) 3.0 规范的 Docket 实例[^3]。 #### 控制器上的注解应用 为了让 Swagger 能够正确解析 RESTful 接口信息,还需要在各个 Controller 方法上加上相应的 Javadoc 注释和支持 Swagger 特性的注解。例如: ```java @Api(tags = "用户管理") @RestController @RequestMapping("/user") public class UserController { @ApiOperation("获取单个用户的详情") @GetMapping("/{id}") public JsonResult getUserById(@PathVariable("id") Long id) throws Exception { // ... } // 更多方法... } ``` 这里利用了 `@Api`, `@ApiOperation` 和其他相关联的元数据标签使得最终生成的文档更加清晰易读。 #### 启动服务后的验证 完成以上所有准备工作之后重启 Spring Boot 应用服务器,并打开浏览器访问 http://localhost:8080/swagger-ui/index.html 即可查看到已成功部署好的交互式 API 文档界面[^1]。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值