SpringBoot 实现微信推送模板的示例代码

 更新时间:2021年12月03日 10:38:56   作者:陈彦斌  
这篇文章主要介绍了SpringBoot 实现微信推送模板,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

导读

  由于最近手头上需要做个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,如果是正式版发送模版消息,这里需要配置你的信息
//                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 "推送失败";
        }
    }
}

去微信公众平台注册一个开发测试账户

  个人开发,我们可以去微信公众号平台注册个测试账户点我直达,微信扫码登录,会给我们一个免费的:appID、appsecret,微信扫码关注公众号,会显示关注测试公众号的用户列表。全局错误码:点我直达

测试

  关注测试公众号,创建模板,并发送指定模板内容

替换模板内容在微信公众平台创建模板

语法:{{变量名.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,如果是正式版发送模版消息,这里需要配置你的信息,替换微信公众号上创建的模板内容
        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 实现微信推送模板的文章就介绍到这了,更多相关SpringBoot 微信推送内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 如何在Java中读取resources下的文件及资源路径

    如何在Java中读取resources下的文件及资源路径

    本文介绍了如何在Java中读取resources下的文件以及获取resource文件的路径,通过使用ClassLoader或Class的getResourceAsStream方法,可以轻松地读取resources目录下的文件,感兴趣的朋友跟随小编一起看看吧
    2023-06-06
  • springboot访问template下的html页面的实现配置

    springboot访问template下的html页面的实现配置

    这篇文章主要介绍了springboot访问template下的html页面的实现配置,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • Java设计模式之java责任链模式详解

    Java设计模式之java责任链模式详解

    这篇文章主要介绍了JAVA 责任链模式的的相关资料,文中讲解非常细致,代码帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2021-09-09
  • java导出数据库的全部表到excel

    java导出数据库的全部表到excel

    这篇文章主要为大家详细介绍了java导出数据库的全部表到excel的相关资料,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-03-03
  • Spring Data JPA框架的Repository自定义实现详解

    Spring Data JPA框架的Repository自定义实现详解

    Spring Data JPA是Spring基于JPA规范的基础上封装的⼀套 JPA 应⽤框架,可使开发者⽤极简的代码即可实现对数据库的访问和操作,本篇我们来了解Spring Data JPA框架的Repository自定义实现
    2022-04-04
  • SpringBoot2底层注解@Configuration配置类详解

    SpringBoot2底层注解@Configuration配置类详解

    这篇文章主要为大家介绍了SpringBoot2底层注解@Configuration配置类详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • Spring Junit单元测试加载配置文件失败问题

    Spring Junit单元测试加载配置文件失败问题

    这篇文章主要介绍了Spring Junit加载配置文件失败问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • DTO 实现 service 和 controller 之间值传递的操作

    DTO 实现 service 和 controller 之间值传递的操作

    这篇文章主要介绍了DTO 实现 service 和 controller 之间值传递的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • 关于mybatis mapper类注入失败的解决方案

    关于mybatis mapper类注入失败的解决方案

    这篇文章主要介绍了关于mybatis mapper类注入失败的解决方案,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • spring boot 配置动态刷新详解

    spring boot 配置动态刷新详解

    这篇文章主要介绍了spring boot 配置动态刷新实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2021-09-09

最新评论