003-读取menu.json文件

一、实体类定义

MenuContext:菜单上下文

@Data
public class MenuContext implements Serializable {
    /**
     * 系统key
     */
    private String systemKey;
    /**
     * 项目key
     */
    private String applicationKey;
    /**
     * 菜单集合
     */
    private List<MenuVO> menus;
}

MenuVO:菜单

@Data
public class MenuVO implements Serializable {
    /**
     * 菜单key
     */
    private String menuKey;
    /**
     * 菜单名称
     */
    private String menuName;
    /**
     * 菜单路径
     */
    private String path;
    /**
     * 排序
     */
    private Integer sort;
    /**
     * 子菜单集合
     */
    private List<MenuVO> childMenu;
    /**
     * 权限集合
     */
    private List<AuthVO> auths;
}

AuthVO:权限

@Data
public class AuthVO implements Serializable {
    /**
     * 菜单key
     */
    private String menuKey;
    /**
     * 权限key
     */
    private String authKey;
    /**
     * 菜单名称
     */
    private String authName;

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        AuthVO authVO = (AuthVO) o;
        return Objects.equals(menuKey, authVO.menuKey) && Objects.equals(authKey, authVO.authKey);
    }

    @Override
    public int hashCode() {
        return Objects.hash(menuKey, authKey);
    }
}

二、读取menu.json文件,保存为MenuContext对象

笔者想在是在SpringBoot项目初始化完毕以后读取权限配置,再保存到数据库,所以写了一个AuthorityConfig类,实现了ApplicationRunner接口。

@Component
public class AuthorityConfig implements ApplicationContextAware, ApplicationRunner {
	@Override
    public void run(ApplicationArguments args) throws Exception {
        // 读取菜单配置
        MenuContext menuContext = buildMenu();
    }

	/**
     * 构建菜单
     */
    private static MenuContext buildMenu() throws IOException {
        ClassPathResource classPathResource = new ClassPathResource("menu/menu.json");
        Scanner scanner = new Scanner(classPathResource.getInputStream(), StandardCharsets.UTF_8.name());
        StringBuilder menuJson = new StringBuilder();
        while (scanner.hasNextLine()) {
            menuJson.append(scanner.nextLine());
        }
        return JSON.parseObject(menuJson.toString(), MenuContext.class);
    }
}

这里构建的逻辑十分的简单,就是读取了menu.json的内容,通过JSON工具包转换成了实体类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值