『博客开发日记』之获取前台配置接口的实现

本文最后更新于 2026年6月6日 下午

获取前台配置接口的实现


获取前台配置接口的需求

先从缓存读取前台配置

只获取前台配置


代码实现

生成代码


添加常量

1
2
3
4
5
6
7
8
/**
* 配置分类:前台
*/
public static final String CONFIG_TYPE_IS_BLOG = "0";
/**
* 通知状态:后台
*/
public static final String CONFIG_TYPE_IS_ADMIN = "1";

在 ConfigController 中添加接口

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 前台系统配置接口
*/
@RestController
@RequestMapping("/config")
@Api(tags = "前台系统配置")
public class ConfigController
{
@Autowired
private ConfigService configService;

@GetMapping("/getBlogConfig")
@SystemLog(businessName = "获取前台系统配置接口")
@ApiOperation(value = "获取前台系统配置接口", notes = "获取博客前台的基本配置")
public ResponseResult getBlogConfig()
{
return configService.getBlogConfig();
}
}


ConfigVo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
* 前后台获取配置VO
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "前后台公共配置响应对象")
public class ConfigVo {

@ApiModelProperty(value = "配置键", example = "site.name")
private String configKey;

@ApiModelProperty(value = "配置值", example = "云梦泽")
private String configValue;
}


在 ConfigServiceImpl 中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//获取后台配置
@Override
@SuppressWarnings("unchecked")
public ResponseResult getSiteConfig()
{
//先从缓存读取后台配置
Set<Object> keys = redisTemplate.keys(RedisKeyConstants.CONFIG_KEY_PREFIX + "*");
List<ConfigVo> configVos = new ArrayList<>();

if (keys != null && !keys.isEmpty()) {
keys.forEach(key -> {
Object value = redisTemplate.opsForValue().get(key);
if (value instanceof ConfigCacheVo configCacheVo && SystemConstants.CONFIG_TYPE_IS_ADMIN.equals(configCacheVo.getConfigType())) {
configVos.add(new ConfigVo(configCacheVo.getConfigKey(), configCacheVo.getConfigValue()));
}
});
}

//缓存为空时再查数据库里的后台配置
if (configVos.isEmpty())
{
LambdaQueryWrapper<Config> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.select(Config::getConfigKey, Config::getConfigValue)
.eq(Config::getConfigType, SystemConstants.CONFIG_TYPE_IS_ADMIN)
.orderByAsc(Config::getId);

List<Config> configList = list(queryWrapper);
configVos.addAll(BeanCopyUtils.copyBeanList(configList, ConfigVo.class));
}

return ResponseResult.okResult(configVos);
}




PS:该系列只做为作者学习开发项目做的笔记用

不一定符合读者来学习,仅供参考


预告

后续会记录博客的开发过程

每次学习会做一份笔记来进行发表

“一花一世界,一叶一菩提”


版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/


『博客开发日记』之获取前台配置接口的实现
http://example.com/2026/05/31/『博客开发日记』之获取前台配置接口的实现/
作者
云梦泽
发布于
2026年5月31日
更新于
2026年6月6日
许可协议