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);
}
}
}
网友评论