新增:新增接口文档配置(基于 Spring Doc OpenAPI,使用 Knife4j 作为增强解决方案)

This commit is contained in:
2022-12-11 21:56:18 +08:00
parent 1e5eaab9d3
commit 79891e8b5a
13 changed files with 366 additions and 44 deletions

View File

@@ -18,18 +18,23 @@ package top.charles7c.cnadmin;
import java.net.InetAddress;
import lombok.SneakyThrows;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import io.swagger.v3.oas.annotations.Hidden;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import top.charles7c.cnadmin.common.config.properties.ContinewAdminProperties;
/**
* 启动程序
*
@@ -39,22 +44,16 @@ import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@SpringBootApplication
@RequiredArgsConstructor
@Import(cn.hutool.extra.spring.SpringUtil.class)
@ComponentScan(basePackages = {"top.charles7c.cnadmin", "cn.hutool.extra.spring"})
public class ContinewAdminApplication {
public class ContinewAdminApplication implements ApplicationRunner {
private static Environment env;
private final ContinewAdminProperties properties;
private final ServerProperties serverProperties;
@SneakyThrows
public static void main(String[] args) {
SpringApplication application = new SpringApplication(ContinewAdminApplication.class);
ConfigurableApplicationContext context = application.run(args);
env = context.getEnvironment();
log.info("------------------------------------------------------");
log.info("{} backend service started successfully.", env.getProperty("continew-admin.name"));
log.info("后端 API 地址http://{}:{}", InetAddress.getLocalHost().getHostAddress(), env.getProperty("server.port"));
log.info("------------------------------------------------------");
SpringApplication.run(ContinewAdminApplication.class, args);
}
/**
@@ -62,8 +61,19 @@ public class ContinewAdminApplication {
*
* @return /
*/
@Hidden
@GetMapping("/")
public String index() {
return String.format("%s backend service started successfully.", env.getProperty("continew-admin.name"));
return String.format("%s backend service started successfully.", properties.getName());
}
@Override
public void run(ApplicationArguments args) throws Exception {
String hostAddress = InetAddress.getLocalHost().getHostAddress();
log.info("------------------------------------------------------");
log.info("{} backend service started successfully.", properties.getName());
log.info("后端 API 地址http://{}:{}", hostAddress, serverProperties.getPort());
log.info("后端 API 文档http://{}:{}/doc.html", hostAddress, serverProperties.getPort());
log.info("------------------------------------------------------");
}
}

View File

@@ -20,6 +20,10 @@ import java.time.Duration;
import lombok.RequiredArgsConstructor;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -39,18 +43,15 @@ import top.charles7c.cnadmin.common.util.RedisUtils;
* @author Charles7c
* @since 2022/12/11 14:00
*/
@Tag(name = "验证码 API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/captcha")
@RequestMapping(value = "/captcha", produces = MediaType.APPLICATION_JSON_VALUE)
public class CaptchaController {
private final CaptchaProperties captchaProperties;
/**
* 获取图片验证码
*
* @return 验证码信息
*/
@Operation(summary = "获取图片验证码", description = "获取图片验证码Base64编码带图片格式data:image/gif;base64")
@GetMapping("/img")
public R<CaptchaVO> getImageCaptcha() {
// 生成验证码

View File

@@ -32,4 +32,9 @@ captcha:
# 宽度
width: 111
# 高度
height: 36
height: 36
--- ### 接口文档配置
springdoc:
swagger-ui:
enabled: true

View File

@@ -32,4 +32,9 @@ captcha:
# 宽度
width: 111
# 高度
height: 36
height: 36
--- ### 接口文档配置
springdoc:
swagger-ui:
enabled: false

View File

@@ -3,9 +3,22 @@ continew-admin:
# 名称
name: ContiNew-Admin
# 应用名称
appName: @project.name@
appName: @project.parent.name@
# 版本
version: @project.version@
# 描述
description: @project.parent.description@
# URL
url: @project.parent.url@
## 作者信息配置
author:
name: Charles7c
email: charles7c@126.com
url: https://blog.charles7c.top/about/me
## 许可协议信息配置
license:
name: Apache-2.0
url: https://github.com/Charles7c/continew-admin/blob/dev/LICENSE
--- ### 日志配置(重叠部分,优先级高于 logback-spring.xml 中的配置)
logging:
@@ -15,6 +28,20 @@ logging:
path: @logging.file.path@
config: classpath:logback-spring.xml
--- ### 接口文档配置
springdoc:
swagger-ui:
path: /swagger-ui.html
tags-sorter: alpha
operations-sorter: alpha
api-docs:
enabled: ${springdoc.swagger-ui.enabled}
path: /v3/api-docs
group-configs:
- group: 'default'
paths-to-match: '/**'
packages-to-scan: top.charles7c.cnadmin.webapi
--- ### 服务器配置
server:
servlet: