feat: 添加swagger,更改springboot版本为3.5.15,去除STR末班字符串
This commit is contained in:
22
pom.xml
22
pom.xml
@@ -6,7 +6,7 @@
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>4.1.0</version>
|
||||
<version>3.5.15</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
@@ -18,14 +18,13 @@
|
||||
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
<maven.compiler.args>--enable-preview</maven.compiler.args>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Web MVC 核心(Boot4 标准) -->
|
||||
<!-- Web MVC 核心 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-webmvc</artifactId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- JPA 持久层 -->
|
||||
@@ -50,10 +49,10 @@
|
||||
<artifactId>spring-boot-starter-validation</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- AOP/AspectJ 【已修正:Boot4 正式名称】 -->
|
||||
<!-- AOP/AspectJ -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aspectj</artifactId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- MySQL 驱动 -->
|
||||
@@ -77,10 +76,10 @@
|
||||
<version>5.8.38</version>
|
||||
</dependency>
|
||||
|
||||
<!-- Sa-Token 权限框架(适配 Boot4) -->
|
||||
<!-- Sa-Token 权限框架(适配 Boot3) -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-spring-boot4-starter</artifactId>
|
||||
<artifactId>sa-token-spring-boot3-starter</artifactId>
|
||||
<version>1.45.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -107,6 +106,13 @@
|
||||
<artifactId>spring-security-crypto</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Swagger / OpenAPI 接口文档 -->
|
||||
<dependency>
|
||||
<groupId>org.springdoc</groupId>
|
||||
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
|
||||
<version>2.8.17</version>
|
||||
</dependency>
|
||||
|
||||
<!-- 统一测试依赖 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.webgame.webgamebackend.common.config;
|
||||
|
||||
import io.swagger.v3.oas.models.OpenAPI;
|
||||
import io.swagger.v3.oas.models.info.Info;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* OpenAPI / Swagger 接口文档配置
|
||||
*/
|
||||
@Configuration
|
||||
public class OpenApiConfig {
|
||||
|
||||
/**
|
||||
* 自定义 OpenAPI 文档基本信息
|
||||
*/
|
||||
@Bean
|
||||
public OpenAPI customOpenAPI() {
|
||||
return new OpenAPI()
|
||||
.info(new Info()
|
||||
.title("WebGame 后端接口文档")
|
||||
.version("1.0.0")
|
||||
.description("WebGame 游戏后端 REST API 接口文档"));
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,10 @@ public class SaTokenConfigure implements WebMvcConfigurer {
|
||||
registry.addInterceptor(new SaInterceptor(handler -> {
|
||||
SaRouter.match("/**")
|
||||
.notMatch("/actuator/**")
|
||||
.notMatch("/swagger-ui/**")
|
||||
.notMatch("/swagger-ui.html")
|
||||
.notMatch("/v3/api-docs/**")
|
||||
.notMatch("/webjars/**")
|
||||
.check(r -> StpUtil.checkLogin());
|
||||
})).addPathPatterns("/**");
|
||||
}
|
||||
|
||||
@@ -9,8 +9,6 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static java.lang.StringTemplate.STR;
|
||||
|
||||
/**
|
||||
* Redis缓存工具类
|
||||
*/
|
||||
@@ -71,7 +69,7 @@ public class RedisCacheUtil {
|
||||
try {
|
||||
json = JSON.toJSONString(v);
|
||||
} catch (Exception e) {
|
||||
log.error(STR."写入缓存失败, 键: \{key}, 原因: \{e.getMessage()}", e);
|
||||
log.error("写入缓存失败, 键: {}, 原因: {}", key, e.getMessage(), e);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -83,7 +81,7 @@ public class RedisCacheUtil {
|
||||
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error(STR."写入缓存失败, 键: \{key}, 值: \{json}, 原因: \{e.getMessage()}", e);
|
||||
log.error("写入缓存失败, 键: {}, 值: {}, 原因: {}", key, json, e.getMessage(), e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -99,7 +97,7 @@ public class RedisCacheUtil {
|
||||
* @return 是否成功
|
||||
*/
|
||||
public Boolean cacheValue(String prefix, String k, Object v, long expireTime) {
|
||||
if (!prefix.endsWith(":")) prefix = STR."\{prefix}:";
|
||||
if (!prefix.endsWith(":")) prefix = prefix + ":";
|
||||
if (k.startsWith(":")) k = k.substring(1);
|
||||
return cacheValue(prefix + k, v, expireTime);
|
||||
}
|
||||
@@ -116,7 +114,7 @@ public class RedisCacheUtil {
|
||||
*/
|
||||
public Boolean cacheValue(String prefix, Long k, Object v, long expireTime) {
|
||||
if (prefix.startsWith(":")) prefix = prefix.substring(1);
|
||||
if (!prefix.endsWith(":")) prefix = STR."\{prefix}:";
|
||||
if (!prefix.endsWith(":")) prefix = prefix + ":";
|
||||
return cacheValue(prefix + k, v, expireTime);
|
||||
}
|
||||
|
||||
@@ -182,7 +180,7 @@ public class RedisCacheUtil {
|
||||
*/
|
||||
public <T> T getCache(String prefix, String k, Class<T> cls, long expireTime) {
|
||||
if (prefix.startsWith(":")) prefix = prefix.substring(1);
|
||||
if (!prefix.endsWith(":")) prefix = STR."\{prefix}:";
|
||||
if (!prefix.endsWith(":")) prefix = prefix + ":";
|
||||
if (k.startsWith(":")) k = k.substring(1);
|
||||
return getCache(prefix + k, cls, expireTime, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
@@ -200,7 +198,7 @@ public class RedisCacheUtil {
|
||||
*/
|
||||
public <T> T getCache(String prefix, Long k, Class<T> cls, long expireTime) {
|
||||
if (prefix.startsWith(":")) prefix = prefix.substring(1);
|
||||
if (!prefix.endsWith(":")) prefix = STR."\{prefix}:";
|
||||
if (!prefix.endsWith(":")) prefix = prefix + ":";
|
||||
return getCache(prefix + k, cls, expireTime, TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
@@ -252,7 +250,7 @@ public class RedisCacheUtil {
|
||||
*/
|
||||
public Boolean removeCache(String prefix, Long k) {
|
||||
if (prefix.startsWith(":")) prefix = prefix.substring(1);
|
||||
if (!prefix.endsWith(":")) prefix = STR."\{prefix}:";
|
||||
if (!prefix.endsWith(":")) prefix = prefix + ":";
|
||||
return removeCache(prefix + k);
|
||||
}
|
||||
|
||||
@@ -289,7 +287,7 @@ public class RedisCacheUtil {
|
||||
*/
|
||||
public Boolean removeCache(String prefix, String k) {
|
||||
if (prefix.startsWith(":")) prefix = prefix.substring(1);
|
||||
if (!prefix.endsWith(":")) prefix = STR."\{prefix}:";
|
||||
if (!prefix.endsWith(":")) prefix = prefix + ":";
|
||||
if (k.startsWith(":")) k = k.substring(1);
|
||||
return removeCache(prefix + k);
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ public class RedisMqUtil {
|
||||
return;
|
||||
}
|
||||
if (message == null) {
|
||||
log.warn(STR."[REDIS消息队列] 发布信息至通道(\{channel})失败: 信息体为 null");
|
||||
log.warn("[REDIS消息队列] 发布信息至通道({})失败: 信息体为 null", channel);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -40,13 +40,13 @@ public class RedisMqUtil {
|
||||
try {
|
||||
json = JSON.toJSONString(message);
|
||||
} catch (Exception e) {
|
||||
log.warn(STR."[REDIS消息队列] 发布信息至通道(\{channel})异常: 信息实体无法正确的序列化为JSON字符串, \{e.getMessage()}", e);
|
||||
log.warn("[REDIS消息队列] 发布信息至通道({})异常: 信息实体无法正确的序列化为JSON字符串, {}", channel, e.getMessage(), e);
|
||||
return;
|
||||
}
|
||||
|
||||
redisTemplate.convertAndSend(channel, json);
|
||||
} catch (Exception e) {
|
||||
log.warn(STR."[REDIS消息队列] 发布信息至通道(\{channel})异常: \{e.getMessage()}", e);
|
||||
log.warn("[REDIS消息队列] 发布信息至通道({})异常: {}", channel, e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,12 +56,12 @@ public class RedisMqUtil {
|
||||
return;
|
||||
}
|
||||
if (callback == null) {
|
||||
log.warn(STR."[REDIS消息队列] 订阅通道(\{channel})失败, 回调方法不可为空");
|
||||
log.warn("[REDIS消息队列] 订阅通道({})失败, 回调方法不可为空", channel);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
var adapter = new MessageListenerAdapter((MessageListener) (message, _) -> {
|
||||
var adapter = new MessageListenerAdapter((MessageListener) (message, pattern) -> {
|
||||
var body = message.getBody();
|
||||
var channelName = new String(message.getChannel());
|
||||
|
||||
@@ -69,22 +69,22 @@ public class RedisMqUtil {
|
||||
try {
|
||||
dto = JSON.parseObject(new String(body), clazz);
|
||||
} catch (Exception e) {
|
||||
log.error(STR."[REDIS消息队列] 消费通道(\{channelName})信息实体类型转换处理异常: \{e.getMessage()}, 信息: \{body}", e);
|
||||
log.error("[REDIS消息队列] 消费通道({})信息实体类型转换处理异常: {}, 信息: {}", channelName, e.getMessage(), body, e);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
callback.accept(dto);
|
||||
} catch (Exception e) {
|
||||
log.error(STR."[REDIS消息队列] 消费通道(\{channelName})信息回调处理异常: \{e.getMessage()}, 信息: \{body}", e);
|
||||
log.error("[REDIS消息队列] 消费通道({})信息回调处理异常: {}, 信息: {}", channelName, e.getMessage(), body, e);
|
||||
}
|
||||
});
|
||||
|
||||
redisMessageListenerContainer.addMessageListener(adapter, new PatternTopic(channel));
|
||||
} catch (Exception e) {
|
||||
log.error(STR."[REDIS消息队列] 订阅通道(\{channel})异常: \{e.getMessage()}", e);
|
||||
log.error("[REDIS消息队列] 订阅通道({})异常: {}", channel, e.getMessage(), e);
|
||||
return;
|
||||
}
|
||||
|
||||
log.info(STR."[REDIS消息队列] 订阅通道(\{channel})成功");
|
||||
log.info("[REDIS消息队列] 订阅通道({})成功", channel);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -82,4 +82,17 @@ sa-token:
|
||||
# token 风格(默认可取值:uuid、simple-uuid、random-32、random-64、random-128、tik)
|
||||
token-style: uuid
|
||||
# 是否输出操作日志
|
||||
is-log: true
|
||||
is-log: true
|
||||
|
||||
############## SpringDoc OpenAPI 配置 ##############
|
||||
springdoc:
|
||||
api-docs:
|
||||
# 显式声明启用,消除启动警告(生产环境建议设为 false)
|
||||
enabled: true
|
||||
swagger-ui:
|
||||
enabled: true
|
||||
path: /swagger-ui.html
|
||||
# 按标签字母排序
|
||||
tags-sorter: alpha
|
||||
# 请求方法字母排序
|
||||
operations-sorter: alpha
|
||||
Reference in New Issue
Block a user