Java微信公众号消息推送

本文档展示了如何配置微信公众号信息,创建配置类以自动注入,并实现发送模板消息的功能。通过配置文件设置appid、secret、token和aesKey,创建WxMpProperties类和WxConfig类进行数据注入。接着在WxMsgPush类中发送模板消息,最后通过Controller层的接口对外提供发送服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、在项目的配置文件中配置公众号信息

# 微信公众号配置
wx:
  appid: **********
  secret: **************
  token: *********************
  aeskey: ****************************

在这里插入图片描述
二、创建公众号类、达到将数据注入的目的

@Component
@ConfigurationProperties(prefix = "wx")
public class WxMpProperties {

    /**
     * 公众号appId
     */
    private String appId;

    /**
     * 公众号appSecret
     */
    private String secret;

    /**
     * 公众号token
     */
    private String token;

    /**
     * 公众号aesKey
     */
    private String aesKey;

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getAesKey() {
        return aesKey;
    }

    public void setAesKey(String aesKey) {
        this.aesKey = aesKey;
    }
}

三、创建配置类将公众号所需对象自动注入

@Configuration
public class WxConfig {
    private final WxMpProperties wxMpProperties;

    /**
     * 构造注入
     *
     * @param wxMpProperties
     */
    WxConfig(WxMpProperties wxMpProperties) {
        this.wxMpProperties = wxMpProperties;
    }

    /**
     * 微信客户端配置存储
     *
     * @return
     */
    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
        // 公众号appId
        configStorage.setAppId(wxMpProperties.getAppId());
        // 公众号appSecret
        configStorage.setSecret(wxMpProperties.getSecret());
        // 公众号Token
        configStorage.setToken(wxMpProperties.getToken());
        // 公众号EncodingAESKey
        configStorage.setAesKey(wxMpProperties.getAesKey());
        return configStorage;
    }

    /**
     * 声明实例
     *
     * @return
     */
    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }
}

三、实现发送信息的功能

@Component
public class WxMsgPush {
    /**
     * 微信公众号API的Service
     */
    private final WxMpService wxMpService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 构造注入
     */
    WxMsgPush(WxMpService wxMpService) {
        this.wxMpService = wxMpService;
    }


    /**
     * 发送微信模板信息
     *
     * @param openId 接受者openId
     * @return 是否推送成功
     */
    public Boolean SendWxMsg(String openId) {
        // 发送模板消息接口
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                // 接收者openid
                .toUser(openId)
                // 模板id
                .templateId("********************************")
                // 模板跳转链接(自定义)
                .url("http://www.baidu.com")
                .build();
        // 添加模板数据
        templateMessage.addData(new WxMpTemplateData("first", "*****", "#FF00FF"))
                .addData(new WxMpTemplateData("keyword1", "2021-01-28至2021-02-07", "#A9A9A9"))
                .addData(new WxMpTemplateData("keyword2", "***", "#FF00FF"))
                .addData(new WxMpTemplateData("keyword3","*****"))
                .addData(new WxMpTemplateData("remark", "这还是个测试", "#000000"));
        String msgId = null;
        try {
            // 发送模板消息
            msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        logger.warn("·==++--·推送微信模板信息:{}·--++==·", msgId != null ? "成功" : "失败");
        return msgId != null;
    }
}

模板id:
在这里插入图片描述
没有可以先添加:
在这里插入图片描述
四、创建Controller层

@RestController
@RequestMapping(value = "/api/wxaccount",method = RequestMethod.POST)
public class SendWxAccountApi {
    /**
     * 微信消息推送
     */
    private WxMsgPush wxMsgPush;

    /**
     * 构造注入
     */
    protected SendWxAccountApi(WxMsgPush wxMsgPush) {
        this.wxMsgPush = wxMsgPush;
    }

    /**
     * 发送微信模板消息
     */
    @ApiOperation("发送微信模板消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "接受者openId", dataType = "String", paramType = "query")
    })
    @PostMapping("sendWxInfo")
    public void sendWxInfo(String openId) {
        // 执行发送
        Boolean aBoolean = wxMsgPush.SendWxMsg(openId);
        System.out.println(aBoolean);
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值