Springboot3.x 与 Springdoc & swagger -- 迁移

一、背景

springboot升级为3.x时,swagger的迁移会出现一些问题,目前springboot3.x将 包javax下的所有内容都迁移到了jakarta下,比如HttpServletRequest, 而swagger还是使用的包javax, 导致出现不兼容的问题,因此可以使用springdoc来替代以前的swagger包.

二、步骤

  1. webmvc只需导入一个jar包
<dependency>
	<groupId>org.springdoc</groupId>
	<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
	<version>2.0.2</version>
</dependency>
  1. yml配置
######## swagger configuration ###############
springdoc:
  packages-to-scan: ##需要扫描的包,可以配置多个
    - com.hmy.azure.controller
  paths-to-exclude:  ##配置不包含在swagger文档中的api
    - /api/test/**
    - /api/mockito/data
  swagger-ui:
    enabled: true  #开启/禁止swagger,prod可以设置为false
    path: /swagger-ui.html  #swagger页面
  api-docs:
    enabled: true #开启/禁止api-docs, prod可以设置为false
    path: /api-docs #api的json文档
  use-management-port: false
  ### 设置为true时, management也需要设置
#  use-management-port: true
#  show-actuator: true

#management:
#  server:
#    port: 9090
#  endpoints:
#    web:
#      exposure:
#        include: openapi,swagger-ui
  1. SpringDocConfiguration 配置文件
package com.hmy.azure.config;

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Contact;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringDocConfiguration {
    @Bean
    public OpenAPI openAPI(){
        return new OpenAPI()
                .info(apiInfo())
                .externalDocs(new ExternalDocumentation()
                        .description("SpringDoc Wiki Documentation")
                        .url("https://springdoc.org/v2"));
    }

    private Info apiInfo() {
        return new Info()
                .title("Azure Demo API Doc")
                .description("springfox swagger 3.0 demo")
                .version("1.0.0")
                .contact(new Contact()
                        .name("<Your name>")
                        .url("<Your url>")
                        .email("<Your email>")
                )
                .license(new License()
                        .name("Apache 2.0")
                        .url("http://www.apache.org/licenses/LICENSE-2.0.txt")
                );
    }
}

  1. UserController类
package com.hmy.azure.controller;

import com.hmy.azure.bean.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Operation(summary = "List users",description = "List users by conditions")
    @Parameters({
            @Parameter(in = ParameterIn.HEADER,name = "idToken",description = "The Authorization token",required = true),
            @Parameter(in = ParameterIn.QUERY,name = "status",description = "The user's status"),
            @Parameter(in = ParameterIn.QUERY,name = "department",description = "The user's department")
    })
    @GetMapping("/list")
    public ResponseEntity<Object> listUser(@RequestParam(required = false) String status,
                                          @RequestParam(required = false) String department){
        return ResponseEntity.ok("SUCCESS");
    }

    @Operation(summary = "Get a user",description = "Get user by id")
    @Parameter(in = ParameterIn.PATH,name = "id",description = "The user's id",required = true)
    @GetMapping("/{id}")
    public ResponseEntity<Object> getUserById(@PathVariable String id){
        return ResponseEntity.ok("SUCCESS");
    }

    @Operation(summary = "Create a user",description = "Create a user")
    @PostMapping
    public ResponseEntity<Object> createUser(@RequestBody User user){
        return ResponseEntity.ok("SUCCESS");
    }
}

三、效果图

P1:
在这里插入图片描述

P2:
参数

P3:
在这里插入图片描述

四、遇到过的问题

问题: 找不到SwaggerWelcomeCommon类, 报错如下

Description:
Parameter 3 of method indexPageTransformer in org.springdoc.webmvc.ui.SwaggerConfig required a bean of type 'org.springdoc.webmvc.ui.SwaggerWelcomeCommon' that could not be found.
Action:
Consider defining a bean of type 'org.springdoc.webmvc.ui.SwaggerWelcomeCommon' in your configuration.

原因: yml中use-management-port设置为了true
解决方法:yml中设置 use-management-port: false
问题参考链接: SwaggerWelcomeCommon could not be found

五、相关链接

  1. 注解的替换及使用见官方文档
    在这里插入图片描述
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
org.springdoc.openapi-gradle-plugin 是一个基于 Gradle 构建工具的插件,用于为基于 Spring 框架的应用程序生成 OpenAPI 文档。通过使用这个插件,开发人员可以方便地将其 Spring Boot 项目转换为遵循 OpenAPI(以前称为 Swagger)规范的文档,并将它们发布到其应用程序的端点上。 该插件简化了将 OpenAPI 规范集成到 Spring Boot 项目中的流程,使开发人员可以轻松地生成、编译和定制 OpenAPI 文档。它还支持自动注解的映射,从而减少了手动配置的工作量。 org.springdoc.openapi-gradle-plugin 提供了一个易于使用的DSL(领域专用语言),让开发人员可以通过 Gradle 构建文件来自定义其生成的 OpenAPI 文档。通过配置插件,开发人员可以定义文档的标题、版本、描述、联系人信息、许可证等内容,还可以选择要包含在文档中的响应码、路径、参数等信息。 使用该插件,开发人员可以更方便地将其 Spring Boot 项目的 API 文档集成到其应用程序中。它还提供了丰富的扩展性和自定义选项,使开发人员能够根据其特定的需求进行定制。 总的来说,org.springdoc.openapi-gradle-plugin 是一个非常有用的工具,可以帮助开发人员将 OpenAPI 规范集成到他们的 Spring Boot 项目中,并生成易于阅读和理解的 API 文档。它简化了这一过程,并提供了丰富的定制选项,让开发人员可以根据其需求对文档进行个性化定制。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值