『博客开发日记-后台』之获取菜单表单数据接口的实现

本文最后更新于 2026年5月5日 晚上

获取菜单表单数据接口的实现


获取菜单表单数据接口的需求

根据菜单id查询表单数据

因为前端 alwaysShow 和 keepAlive 是布尔型

而数据库里的是字符型

所以要对这两个字段进行类型转换


代码实现

创建 MenuFormDetailVo

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "菜单详情响应对象")
public class MenuFormDetailVo
{
@ApiModelProperty(value = "菜单ID", example = "1")
private Long id;

@ApiModelProperty(value = "父菜单ID", example = "0")
private Long parentId;

@ApiModelProperty(value = "菜单名称", example = "字典管理")
private String name;

@ApiModelProperty(value = "菜单类型(C-目录 M-菜单 B-按钮)", example = "M")
private String type;

@ApiModelProperty(value = "路由字符串", example = "dict")
private String path;

@ApiModelProperty(value = "路由名称(前端路由名)", example = "Dict")
private String routeName;

@ApiModelProperty(value = "路由路径(可用于自定义路由字段)", example = "dict")
private String routePath;

@ApiModelProperty(value = "跳转路径", example = "/system/dict")
private String redirect;

@ApiModelProperty(value = "组件路径", example = "system/dict/index")
private String component;

@ApiModelProperty(value = "ICON", example = "dict")
private String icon;

@ApiModelProperty(value = "排序", example = "5")
private Integer sort;

@ApiModelProperty(value = "菜单是否可见(0:显示;1:隐藏)", example = "0")
private String visible;

@ApiModelProperty(value = "权限标识", example = "sys:config:query")
private String perm;

@ApiModelProperty(value = "目录是否始终显示", example = "false")
private String alwaysShow;

@ApiModelProperty(value = "是否缓存(keepAlive)", example = "true")
private String keepAlive;

@ApiModelProperty(value = "路由参数")
private List<MenuParamVo> params;

}

创建 MenuParamVo

1
2
3
4
5
6
7
8
9
10
11
12
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(description = "菜单路由参数响应对象")
public class MenuParamVo
{
@ApiModelProperty(value = "参数名称", example = "from")
private String key;

@ApiModelProperty(value = "参数值", example = "admin")
private String value;
}

在 SystemConstants 中添加几个常量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public static final String INVISIBLE = "1";
/**
* 目录始终显示
*/
public static final String CONTENTS_ALWAYS_SHOW = "0";
/**
* 目录不显示
*/
public static final String CONTENTS_HIDE = "1";
/**
* 缓存 keepAlive
*/
public static final String CACHE_KEEP_ALIVE = "0";
/**
* 不缓存 keepAlive
*/
public static final String NOT_CACHE_KEEP_ALIVE = "1";

在 SysMenuServiceImpl 中实现功能

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
 ///获取菜单表单详情
@Override
public ResponseResult<MenuFormDetailVo> getMenuFormDetail(Long id)
{
//根据菜单id查询表单数据
SysMenu menu = getById(id);

//转换成Vo
MenuFormDetailVo menuFormDetailVo = BeanCopyUtils.copyBean(menu, MenuFormDetailVo.class);

//转换 params 字段:JSON String -> List<MenuParamVo>
if (StringUtils.hasText(menu.getParams())) {
try {
List<MenuParamVo> paramList = JSON.parseObject(
menu.getParams(),
new TypeReference<>() {}
);
menuFormDetailVo.setParams(paramList);
} catch (Exception e) {
//如果解析失败,设置为空列表
menuFormDetailVo.setParams(new ArrayList<>());
}
} else {
menuFormDetailVo.setParams(new ArrayList<>());
}

return ResponseResult.okResult(menuFormDetailVo);
}



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

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


预告

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

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

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


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


『博客开发日记-后台』之获取菜单表单数据接口的实现
http://example.com/2026/05/05/『博客开发日记-后台』之获取菜单表单数据接口的实现/
作者
云梦泽
发布于
2026年5月5日
更新于
2026年5月5日
许可协议