欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot?實現(xiàn)微信推送模板的示例代碼

 更新時間:2021年12月03日 10:38:56   作者:陳彥斌  
這篇文章主要介紹了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ù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 如何在Java中讀取resources下的文件及資源路徑

    如何在Java中讀取resources下的文件及資源路徑

    本文介紹了如何在Java中讀取resources下的文件以及獲取resource文件的路徑,通過使用ClassLoader或Class的getResourceAsStream方法,可以輕松地讀取resources目錄下的文件,感興趣的朋友跟隨小編一起看看吧
    2023-06-06
  • springboot訪問template下的html頁面的實現(xiàn)配置

    springboot訪問template下的html頁面的實現(xiàn)配置

    這篇文章主要介紹了springboot訪問template下的html頁面的實現(xiàn)配置,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-12-12
  • Java設計模式之java責任鏈模式詳解

    Java設計模式之java責任鏈模式詳解

    這篇文章主要介紹了JAVA 責任鏈模式的的相關資料,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2021-09-09
  • java導出數(shù)據(jù)庫的全部表到excel

    java導出數(shù)據(jù)庫的全部表到excel

    這篇文章主要為大家詳細介紹了java導出數(shù)據(jù)庫的全部表到excel的相關資料,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-03-03
  • Spring?Data?JPA框架的Repository自定義實現(xiàn)詳解

    Spring?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-04
  • SpringBoot2底層注解@Configuration配置類詳解

    SpringBoot2底層注解@Configuration配置類詳解

    這篇文章主要為大家介紹了SpringBoot2底層注解@Configuration配置類詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-05-05
  • Spring Junit單元測試加載配置文件失敗問題

    Spring Junit單元測試加載配置文件失敗問題

    這篇文章主要介紹了Spring Junit加載配置文件失敗問題解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-05-05
  • DTO 實現(xiàn) service 和 controller 之間值傳遞的操作

    DTO 實現(xiàn) service 和 controller 之間值傳遞的操作

    這篇文章主要介紹了DTO 實現(xiàn) service 和 controller 之間值傳遞的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 關于mybatis mapper類注入失敗的解決方案

    關于mybatis mapper類注入失敗的解決方案

    這篇文章主要介紹了關于mybatis mapper類注入失敗的解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • spring boot 配置動態(tài)刷新詳解

    spring boot 配置動態(tài)刷新詳解

    這篇文章主要介紹了spring boot 配置動態(tài)刷新實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2021-09-09

最新評論