SpringBoot应用启动成功后主动发送通知给企业微信
2022年2月16日
需求
SpringBoot应用启动后能主动发送一条消息到企业微信的机器人当中,通过此方式主动告知程序已经完成启动。
解决思路
监听springBoot启动完成后,调用企业微信API接口发送通知即可
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
public void serverStarted() { try { String profile = environment.getActiveProfiles()[0]; if ("dev".equals(profile)) { log.info("本地服务启动成功,不发送通知"); return; } Map<String, Object> m = new HashMap<>(); m.put("msgtype", "markdown"); Map<String, Object> content = new HashMap<>(); String contentStr = "环境【" + environment.getActiveProfiles()[0] + "】服务已经启动成功!\n " + ">服务名称: process-node \n" + ">启动时间:" + DateUtil.now(); content.put("content", contentStr); m.put("markdown", content); String jsonStr = JSONUtil.toJsonStr(m); String resp = HttpUtil.post("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxx(此处改为企微机器人通知地址)", jsonStr); } catch (Exception e) { log.error("机器人通知失败", e); } } |