美文网首页开源框架-SpringBoot系列
SpringBoot集成钉钉报警sdk(解决Failed to

SpringBoot集成钉钉报警sdk(解决Failed to

作者: 小胖学编程 | 来源:发表于2020-09-24 13:54 被阅读0次

    1. pom文件配置

    resources/lib目录下加入钉钉的sdk的jar包。

    链接: https://pan.baidu.com/s/11gor6cfrHPBkQcWSvJOYvQ 密码: 0kd4

    <dependency>
        <groupId>com.dingtalk.api</groupId>
        <artifactId>dingtalk</artifactId>
        <version>3.0.12</version>
        <scope>system</scope>
        <systemPath>${project.basedir}/src/main/resources/lib/taobao-sdk-java-auto_1479188381469-20191122.jar
        </systemPath>
    </dependency>
    

    maven插件配置:

    <plugins>
         <plugin>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-maven-plugin</artifactId>
             // ++++++++ 添加这部分配置
             <configuration>
                 <includeSystemScope>true</includeSystemScope>
             </configuration>
             // ++++++++ 添加这部分配置
         </plugin>
     </plugins>
    

    注意:如果部署到服务器上,但是没有配置maven插件,在Spring Bean中使引用sdk文件,就会导致Failed to introspect Class [xxx] from ClassLoader [org.springframework.boot.loader.LaunchedURLClassLoader@492691d7]而实际上,这个异常出现的原因就是:Spring在加载bean时,找不到对应的Class文件。

    2. 代码编写

    @Slf4j
    public class DingtalkUtils {
        /**
         * 钉钉群消息推送
         *
         * @param webHook 钉钉生成的访问地址
         * @param content 要通知的内容
         */
        public static void dingtalk(String webHook, String content, String atMobiles) {
            try{
                DingTalkClient client = new DefaultDingTalkClient(webHook);
                OapiRobotSendRequest request = new OapiRobotSendRequest();
                request.setMsgtype("text");
                OapiRobotSendRequest.Text text = new OapiRobotSendRequest.Text();
                text.setContent(content);
                request.setText(text);
                if(atMobiles!=null) {
                    OapiRobotSendRequest.At at = new OapiRobotSendRequest.At();
                    at.setAtMobiles(Arrays.asList(atMobiles.split(",")));
                    request.setAt(at);
                }
                client.execute(request);
            } catch (Exception e) {
                log.error("Alter to DingTalk error.", e);
            }
        }
    }
    

    推荐阅读

    钉钉开放平台

    Springboot Maven项目引入钉钉机器人jar包(SDK)遇到的问题

    相关文章

      网友评论

        本文标题:SpringBoot集成钉钉报警sdk(解决Failed to

        本文链接:https://www.haomeiwen.com/subject/ujbzyktx.html