从Swagger到springdoc-openapi的替换过程(处理springboot3.x版本与Swagger不兼容的问题)

跟着一本有些年头的书学习springboot,书里用的是springboot2.x和Swagger来生成接口文档。
但现在的SpringBoot3.x已经不能使用Swagger,于是把怎么样使用springdoc-openapi替换Swagger的过程记录一下。

原本的代码:

依赖:

        <dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>
		<!--Swagger-UI依赖 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.9.2</version>
		</dependency>

SpringBoot Application:

@SpringBootApplication
//↓这个注解是开启Swagger支持
@EnableSwagger2
public class SwaggerApplication {

	public static void main(String[] args) {
		SpringApplication.run(SwaggerApplication.class, args);
	}

}

Swagger配置文件:


/**
 * Swagger 配置文件
 */
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //↓ 这里指明了扫描包的位置
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(" RESTful APIs")
                .description("RESTful APIs")
                .termsOfServiceUrl("http://localhost:8080/")
                .contact("long")
                .version("1.0")
                .build();
    }
}

Controller:(注意这里包的位置)

package com.example.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RestController
public class HelloWorldController {
    @ApiOperation(value = "hello", notes = "notes ")
    @RequestMapping("/hello")
    public String hello() throws Exception {
        return "HelloWorld ,Spring Boot!";
    }
    //使用该注解忽略这个API
    @ApiIgnore
    @RequestMapping(value = "/ignoreApi")
    public String  ignoreApi() {
        return "HelloWorld ,Spring Boot!";
    }
    
}

在SpringBoot2.x以及jdk1.8的环境下,是可以正常运行的,结果如下:

启动程序后,访问这个连接就可以Swagger UIicon-default.png?t=O83Ahttp://127.0.0.1:8080/swagger-ui.html#/

但是!!!SpringBoot3.x是不行的,下面开始替换过程:

首先是修改依赖:

<!--    springboot3+ 和jdk17+  和swagger版本不兼容  -->
        <!--Swagger依赖-->
<!--        <dependency>-->
<!--            <groupId>io.springfox</groupId>-->
<!--            <artifactId>springfox-swagger2</artifactId>-->
<!--            <version>2.9.2</version>-->
<!--        </dependency>-->
<!--        &lt;!&ndash;Swagger-UI依赖 &ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>io.springfox</groupId>-->
<!--            <artifactId>springfox-swagger-ui</artifactId>-->
<!--            <version>2.9.2</version>-->
<!--        </dependency>-->
       <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.6.0</version>
        </dependency>

在application.properties添加关于文档的配置项:

springdoc.api-docs.enabled=true
springdoc.api-docs.path=/v3/api-docs
springdoc.swagger-ui.enabled=true
springdoc.swagger-ui.config-url= /swagger-ui/index.html

在之前Swagger的配置类中,使用下面的代码替换之前的,这是openAPI的配置方式: 

 @Bean
    public OpenAPI customOpenAPI() {
        return new OpenAPI()
                .info(new Info().title("My RESTful APIs")
                        .description("API documentation with OpenAPI")
                        .version("1.0")
                        .contact(new Contact().name("yydwjj")
                                .url("https://example.com")
                                .email("yydwjj@example.com")));
    }

    // 定义 API 分组和路径扫描,相当于 Swagger 2 中的 basePackage 和 paths 配置
    @Bean
    public GroupedOpenApi publicApi() {
        return GroupedOpenApi.builder()
                .group("public-api")
                .packagesToScan("com.yydwjj.studentsystem.controller")
                .pathsToMatch("/**")
                .build();
    }

就ok了,启动程序

测试链接:

Swagger UIicon-default.png?t=O83Ahttp://localhost:8080/swagger-ui/index.html

在Swagger-ui中,输入自己设定过的路径:(properties文件中的springdoc.api-docs.path

 就可以看到生成的接口文档了

测试链接2:

localhost:8080/v3/api-docs

迁移完成!

参考:

用于 spring-boot 的 OpenAPI 3 库

### 如何在Spring项目中使用Swagger UI(通过`springdoc-openapi-ui`) 为了在Spring项目中集成并配置Swagger UI,可以采用`springdoc-openapi-ui`库。此方法适用于Spring Boot 3.x版本及其以上。 #### 添加Maven依赖 首先,在项目的`pom.xml`文件内加入如下依赖: ```xml <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.0.4</version> </dependency> ``` 上述代码片段展示了如何引入必要的构建工具依赖项以支持OpenAPI 3标准下的接口自动生成展示功能[^1]。 #### 配置应用属性 接着,可以在`application.properties`或`application.yml`中设置一些基本参数来定制化Swagger界面的行为表现形式: 对于`.properties`格式: ```properties springdoc.api-docs.path=/v3/api-docs springdoc.swagger-ui.path=/swagger-ui.html ``` 而对于`.yml`格式,则应写作: ```yaml springdoc: api-docs: path: /v3/api-docs swagger-ui: path: /swagger-ui.html ``` 这些配置指定了API文档以及UI页面的具体访问路径[^3]。 #### 启动类注解 确保主应用程序启动类上已标注有`@EnableWebMvc`或其他适当启用MVC特性的注释;不过通常情况下,默认创建的Spring Boot项目已经包含了这一点。如果需要进一步控制RESTful服务行为,还可以考虑添加其他相关注释。 #### 测试验证 完成上述步骤之后,重启服务器,并尝试打开浏览器访问指定URL地址查看效果。默认情况下,可以通过`http://localhost:<port>/swagger-ui.html`进入交互式的API文档浏览环境[^5]。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值