本文最后更新于 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
|
@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/