微信开发系列 — — 微信模板消息
前言:
微信系列的开发,网上已经有一些很不错的工具支持了,帮助我们快速开发微信应用。今天介绍一个借助微信工具快速开发微信模板消息。
首先我们需要引入一些jar包:
<dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>2.9.8.BETA</version> </dependency>
开始开发:
第一步:
准备一个配置文件类。用来配置需要用到的APPID,TEMPLATEID等
package com.wen.demo.config;
public interface WeChatConfig {
/**
* 测试号或公众号ID
*/
String APPID="";
/**
* 测试号或公众号密码
*/
String SECRET="";
/**
* 发送到哪个用户的ID
*/
String USERID="";
/**
* 模板消息ID
*/
String TEMPLATEID="";
}
以上加入自己的各种ID即可随时使用
第二步:
准备一个bean,主要是实例化,配置相关的APPID,secret等。
package com.wen.demo.config;
import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Component
public class WechatMpConfig {
/**
* 微信服务装载
* @return
*/
@Bean
public WxMpService wxMpService() {
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
return wxMpService;
}
/**
* 配置温馨的APPID和密码
* @return
*/
@Bean
public WxMpConfigStorage wxMpConfigStorage() {
WxMpInMemoryConfigStorage wxMpConfigStorage = new WxMpInMemoryConfigStorage();
wxMpConfigStorage.setAppId(WeChatConfig.APPID);
wxMpConfigStorage.setSecret(WeChatConfig.SECRET);
return wxMpConfigStorage;
}
}
第三步:
写一个controller,来发送模板消息
package com.wen.demo.controller;
import com.wen.demo.config.WeChatConfig;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
@RestController
public class SendMessageByWX {
@Autowired
private WxMpService wxMpService;
/**
* 发送模板消息控制器
* @return
*/
@GetMapping(value = "send")
public String send() {
//实例化模板对象
WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();
//设置模板ID
wxMpTemplateMessage.setTemplateId(WeChatConfig.TEMPLATEID);
//设置发送给哪个用户
wxMpTemplateMessage.setToUser(WeChatConfig.USERID);
//构建消息格式
List<WxMpTemplateData> list= Arrays.asList(
new WxMpTemplateData("first","wen"),
new WxMpTemplateData("keyword1","wen"),
new WxMpTemplateData("keyword2","wen"),
new WxMpTemplateData("keyword3","wen"),
new WxMpTemplateData("keyword4","wen"),
new WxMpTemplateData("remark","wen")
);
//放进模板对象。准备发送
wxMpTemplateMessage.setData(list);
try {
//发送模板
wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
} catch (WxErrorException e) {
e.printStackTrace();
}
return "发送成功";
}
}
第四步:
就是开始测试啦,启动服务器。直接访问
打开你的测试号,基本就可以了。。不过注意一点是。测试号需要配置相关的东西,而且需要和你的发送的字段名一样,否则会出错。如果不好测试号,请看我的第一篇文章,网页授权。
总结:
好好学习,不然怎么泡妞是吧~!

浙公网安备 33010602011771号