SpringBoot?實現(xiàn)微信推送模板的示例代碼
導讀
由于最近手頭上需要做個Message Gateway,涉及到:郵件(點我直達)、短信、公眾號等推送功能,網上學習下,整理下來以備以后使用。
添加依賴
在SpringBoot項目中添加依賴
<!--微信模版消息推送三方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 { /* * 微信測試賬號推送 * */ @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/")//點擊模版消息要訪問的網址 .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 "推送失敗"; } } }
去微信公眾平臺注冊一個開發(fā)測試賬戶
個人開發(fā),我們可以去微信公眾號平臺注冊個測試賬戶點我直達,微信掃碼登錄,會給我們一個免費的:appID、appsecret,微信掃碼關注公眾號,會顯示關注測試公眾號的用戶列表。全局錯誤碼:點我直達
測試
關注測試公眾號,創(chuàng)建模板,并發(fā)送指定模板內容
替換模板內容在微信公眾平臺創(chuàng)建模板
語法:{{變量名.DATA}}
姓名:{{user_name.DATA}}
性別:{{sex.DATA}}
手機號:{{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 { /* * 微信測試賬號推送 * */ @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/")//點擊模版消息要訪問的網址 .build(); //3,如果是正式版發(fā)送模版消息,這里需要配置你的信息,替換微信公眾號上創(chuàng)建的模板內容 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 "推送失敗"; } } }
到此這篇關于SpringBoot?實現(xiàn)微信推送模板的文章就介紹到這了,更多相關SpringBoot?微信推送內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot訪問template下的html頁面的實現(xiàn)配置
這篇文章主要介紹了springboot訪問template下的html頁面的實現(xiàn)配置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-12-12Spring?Data?JPA框架的Repository自定義實現(xiàn)詳解
Spring?Data?JPA是Spring基于JPA規(guī)范的基礎上封裝的?套?JPA?應?框架,可使開發(fā)者?極簡的代碼即可實現(xiàn)對數(shù)據(jù)庫的訪問和操作,本篇我們來了解Spring?Data?JPA框架的Repository自定義實現(xiàn)2022-04-04SpringBoot2底層注解@Configuration配置類詳解
這篇文章主要為大家介紹了SpringBoot2底層注解@Configuration配置類詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05DTO 實現(xiàn) service 和 controller 之間值傳遞的操作
這篇文章主要介紹了DTO 實現(xiàn) service 和 controller 之間值傳遞的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02