一、添加依赖
在SpringBoot 项目 pom 配置文件中添加依赖
<!--微信模版消息推送三方sdk-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.3.0</version>
</dependency>
二、控制层代码
package com.demo.springbootwechart.controller;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Description:微信信息推送
*/
@RestController
public class wxMessageController {
/*
* 微信信息推送
* */
@GetMapping("/sendMessage")
public String sendMessage() {
//1,配置
WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
wxStorage.setAppId("AppId");
wxStorage.setSecret("appsecret");
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxStorage);
//2,推送消息
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.toUser(openid)//要推送的用户openid
.templateId(messageTemplateId)//模版id
.url(openUrl)//点击模版消息要访问的网址
.build();
try {
wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
return "发送成功";
} catch (Exception e) {
System.out.println("发送失败:" + e.getMessage());
e.printStackTrace();
return "发送失败";
}
}
}
到微信公众平台注册一个开发测试账户:
地址:http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
获得:appID、appsecret
三、测试
关注测试公众号,创建模板,并发送指定模板内容
创建模板内容:
Tg :语法:{{变量名.DATA}}
举傈子:
使用时,替换模板内容
姓名:{{user_name.DATA}}
性别:{{sex.DATA}}
手机号:{{phone.DATA}}
邮箱:{{email.DATA}}
四、控制层修改
package com.demo.springbootwechart.controller;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Objects;
/**
* @Description:微信信息推送
*/
@RestController
public class wxMessageController {
/*
* 微信信息推送
* */
@GetMapping("/sendMessage")
public String sendMessage() {
//1,配置
WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
wxStorage.setAppId("wxa3094b397b103a47");
wxStorage.setSecret("7df98f46b68f6aefb00db6a770a9f348");
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxStorage);
//2,推送消息
WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
.toUser("oPZ0T5wF97VBvTkl18SeBmvn2EYI")//要推送的用户openid
.templateId(“jhLze3FPEPz_kWsWmvnWX8mPuBIT0Leq6RrBQGo8Gk8”")//模版id
.url(openUrl)//点击模版消息要访问的网址
.build();
//3,如果是正式版发送模版消息,这里需要配置你的信息,替换微信公众号上创建的模板内容
templateMessage.addData(new WxMpTemplateData("user_name", "张三", "#CCCCFF"));
templateMessage.addData(new WxMpTemplateData("sex", "男", "#FF00FF"));
templateMessage.addData(new WxMpTemplateData("phone", "188888888888", "#CCFF99"));
templateMessage.addData(new WxMpTemplateData("email", "752555212@qq.com", "#FF0033"));
try {
wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
return "发送成功";
} catch (Exception e) {
System.out.println("发送失败:" + e.getMessage());
e.printStackTrace();
return "发送失败";
}
}
}