springboot3 + java21框架之SpringDoc,一种swagger替代方案
前言
因swagger停更,随着springboot版本的升级,兼容性越来越差。springboot3后,更是无法使用。今天我们聊一聊springboot3 + java21下SpringDoc的集成。
一、技术栈
技术栈 | 版本 |
---|---|
springboot | 3.3.2 |
jdk | 21 |
springdoc | 2.6.0 |
二、pom依赖
<!--spring-boot依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.3.2</version>
</dependency>
<!-- SpringDoc starter -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.6.0</version> <!-- 确保使用的是适合Spring Boot 3.x的版本 -->
</dependency>
二、yml配置
springdoc:
api-docs:
enabled: true
#接口地址
path: /api-docs
swagger-ui:
#swagger-ui地址,即页面访问地址
path: /doc/swagger.html
#扫描接口包。由它决定哪些包的接口会出现的接口文档中
packages-to-scan: com.coxiang.horizon.web.controller
三、配置类
package com.coxiang.horizon.common.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 OpenApiConfig {
@Bean
public OpenAPI myOpenAPI() {
return new OpenAPI()
.info(new Info()
.title("地平线API")
.description("地平线")
.version("v1.0.0")
.license(new License().name("许可协议").url("https://coxiang.com"))
.contact(new Contact().name("durango").email("aose0827@163.com")))
.externalDocs(new ExternalDocumentation().description("xxx").url("https://coxiang.com"));
}
}
四、Controller
package com.coxiang.horizon.web.controller;
import com.coxiang.horizon.api.IRedisService;
import com.coxiang.horizon.dao.entity.CbBasic;
import com.coxiang.horizon.dao.service.ICbBasicService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Tag(name = "债券基本信息控制器", description = "债券基本信息API")
@RestController
@RequestMapping("/cb")
public class CbBasicController {
@Resource
private ICbBasicService cbBasicService;
@Resource
private IRedisService redisService;
@Cacheable(value = "cb", key = "#tsCode")
@Operation(summary = "获取债券基本信息1")
@GetMapping("/basic")
public CbBasic getBasic(@RequestParam String tsCode) {
CbBasic cbBasic = cbBasicService.getById(tsCode);
return cbBasic;
}
@Operation(summary = "获取债券基本信息2")
@GetMapping("/basic2")
public CbBasic getBasic2(@RequestParam String tsCode) {
CbBasic cbBasic = (CbBasic) redisService.get(tsCode);
if (cbBasic != null) {
return cbBasic;
} else {
cbBasic = cbBasicService.getById(tsCode);
redisService.set(tsCode, cbBasic);
}
return cbBasic;
}
}
五、效果
地址:http://127.0.0.1:8080/doc/swagger-ui/index.html#/