SpringBoot?實(shí)現(xiàn)微信推送模板的示例代碼
導(dǎo)讀
由于最近手頭上需要做個(gè)Message Gateway,涉及到:郵件(點(diǎn)我直達(dá))、短信、公眾號(hào)等推送功能,網(wǎng)上學(xué)習(xí)下,整理下來以備以后使用。
添加依賴
在SpringBoot項(xiàng)目中添加依賴
<!--微信模版消息推送三方sdk--> <dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>3.3.0</version> </dependency>
控制層代碼
package com.ybchen.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; /** * @ClassName:PushController * @Description:微信推送 * @Author:chenyb * @Date:2020/11/27 10:33 上午 * @Versiion:1.0 */ @RestController public class PushController { /* * 微信測(cè)試賬號(hào)推送 * */ @GetMapping("/push") public String push() { //1,配置 WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage(); wxStorage.setAppId("AppId"); wxStorage.setSecret("Secret"); WxMpService wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxStorage); //2,推送消息 WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder() .toUser("ojPPk54RcFkCgGVP3m66v1RM2mvA")//要推送的用戶openid .templateId("a7RPsASc7fw33zFo7zEfWKE0vrPnUo7VZ82fX3tTfMg")//模版id .url("https://www.cnblogs.com/chenyanbin/")//點(diǎn)擊模版消息要訪問的網(wǎng)址 .build(); //3,如果是正式版發(fā)送模版消息,這里需要配置你的信息 // templateMessage.addData(new WxMpTemplateData("name", "value", "#FF00FF")); // templateMessage.addData(new WxMpTemplateData(name2, value2, color2)); try { wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage); return "推送成功"; } catch (Exception e) { System.out.println("推送失?。? + e.getMessage()); e.printStackTrace(); return "推送失敗"; } } }
去微信公眾平臺(tái)注冊(cè)一個(gè)開發(fā)測(cè)試賬戶
個(gè)人開發(fā),我們可以去微信公眾號(hào)平臺(tái)注冊(cè)個(gè)測(cè)試賬戶點(diǎn)我直達(dá),微信掃碼登錄,會(huì)給我們一個(gè)免費(fèi)的:appID、appsecret,微信掃碼關(guān)注公眾號(hào),會(huì)顯示關(guān)注測(cè)試公眾號(hào)的用戶列表。全局錯(cuò)誤碼:點(diǎn)我直達(dá)
測(cè)試
關(guān)注測(cè)試公眾號(hào),創(chuàng)建模板,并發(fā)送指定模板內(nèi)容
替換模板內(nèi)容在微信公眾平臺(tái)創(chuàng)建模板
語法:{{變量名.DATA}}
姓名:{{user_name.DATA}}
性別:{{sex.DATA}}
手機(jī)號(hào):{{phone.DATA}}
郵箱:{{email.DATA}}
控制層修改
package com.ybchen.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; /** * @ClassName:PushController * @Description:微信推送 * @Author:chenyb * @Date:2020/11/27 10:33 上午 * @Versiion:1.0 */ @RestController public class PushController { /* * 微信測(cè)試賬號(hào)推送 * */ @GetMapping("/push") public String push() { //1,配置 WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage(); wxStorage.setAppId("wx12db1518efd2302c"); wxStorage.setSecret("056f31d80a5a22cc0c418cc08f5657ad"); WxMpService wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxStorage); //2,推送消息 WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder() .toUser("ojPPk54RcFkCgGVP3m66v1RM2mvA")//要推送的用戶openid .templateId("O0t0lPP7xRqbNz0-OwPzliSplzGFrkr4-au-OIGhiOE")//模版id .url("https://www.cnblogs.com/chenyanbin/")//點(diǎn)擊模版消息要訪問的網(wǎng)址 .build(); //3,如果是正式版發(fā)送模版消息,這里需要配置你的信息,替換微信公眾號(hào)上創(chuàng)建的模板內(nèi)容 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", "543210188@qq.com", "#FF0033")); try { wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage); return "推送成功"; } catch (Exception e) { System.out.println("推送失敗:" + e.getMessage()); e.printStackTrace(); return "推送失敗"; } } }
到此這篇關(guān)于SpringBoot?實(shí)現(xiàn)微信推送模板的文章就介紹到這了,更多相關(guān)SpringBoot?微信推送內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot訪問template下的html頁面的實(shí)現(xiàn)配置
這篇文章主要介紹了springboot訪問template下的html頁面的實(shí)現(xiàn)配置,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Java設(shè)計(jì)模式之java責(zé)任鏈模式詳解
這篇文章主要介紹了JAVA 責(zé)任鏈模式的的相關(guān)資料,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2021-09-09java導(dǎo)出數(shù)據(jù)庫的全部表到excel
這篇文章主要為大家詳細(xì)介紹了java導(dǎo)出數(shù)據(jù)庫的全部表到excel的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03Spring?Data?JPA框架的Repository自定義實(shí)現(xiàn)詳解
Spring?Data?JPA是Spring基于JPA規(guī)范的基礎(chǔ)上封裝的?套?JPA?應(yīng)?框架,可使開發(fā)者?極簡(jiǎn)的代碼即可實(shí)現(xiàn)對(duì)數(shù)據(jù)庫的訪問和操作,本篇我們來了解Spring?Data?JPA框架的Repository自定義實(shí)現(xiàn)2022-04-04SpringBoot2底層注解@Configuration配置類詳解
這篇文章主要為大家介紹了SpringBoot2底層注解@Configuration配置類詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Spring Junit單元測(cè)試加載配置文件失敗問題
這篇文章主要介紹了Spring Junit加載配置文件失敗問題解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05DTO 實(shí)現(xiàn) service 和 controller 之間值傳遞的操作
這篇文章主要介紹了DTO 實(shí)現(xiàn) service 和 controller 之間值傳遞的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-02-02關(guān)于mybatis mapper類注入失敗的解決方案
這篇文章主要介紹了關(guān)于mybatis mapper類注入失敗的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04spring boot 配置動(dòng)態(tài)刷新詳解
這篇文章主要介紹了spring boot 配置動(dòng)態(tài)刷新實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2021-09-09