栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java对接微信

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java对接微信

java对接微信不同平台需要的jar

步骤:这里以对接微信公众号为例

        1.引入jar

        
   
           com.github.binarywang
           weixin-java-mp
   3.6.0

        2.配置公众号信息

# 微信公众号配置
wx:
      appid: 11111
      secret: 22222
      token: 33333
      aeskey: 44444 

        3.代码编写

第一步:对应配置文件的实体类

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

    
    private String appId;

    
    private String secret;

    
    private String token;

    
    private String aesKey;
}

第二步:微信客户端配置和WxMpService引入

package com.ruoyi.project.yf.supply.res;

import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


@Configuration
public class WxConfig {
    @Value("${wx.wx-baseUrl}")
    private String baseUrl;
    @Value("${wx.wx-appid}")
    private String APP_ID;
    @Value("${wx.wx-appsecret}")
    private String APP_SECRET;
    
    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }


    
    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
        // 公众号appId
        configStorage.setAppId(APP_ID);
        // 公众号appSecret
        configStorage.setSecret(APP_SECRET);
        // 公众号Token
        //configStorage.setToken("");
        // 公众号EncodingAESKey
        //configStorage.setAesKey(wxMpProperties.getAesKey());
        return configStorage;
    }
}

第三步:消息推送接口

@Autowired
private WxMpService wxMpService;

WxMpTemplateMessage wxMpTemplateMessage = new WxMpTemplateMessage();
//设置模板ID
wxMpTemplateMessage.setTemplateId(TID);
//设置发送给哪个用户
String openid = item.getOpenid();
wxMpTemplateMessage.setToUser(openid);

//构建消息格式
List listData = Arrays.asList(
                                new WxMpTemplateData("first", "尊敬的物流:"),
                                new WxMpTemplateData("keyword1", "123456"),
                                new WxMpTemplateData("keyword2","123"),
                                new WxMpTemplateData("keyword3", "发货通知"),
                                new WxMpTemplateData("remark","请注意及时接单")
                        );
//放进模板对象。准备发送
wxMpTemplateMessage.setData(listData);
//接收发送模板消息结果,就是msgid,if(msgid! = null)即成功
String wxTemplateResult = null;

try {
  //发送模板
  wxTemplateResult =         
  wxMpService.getTemplateMsgService().sendTemplateMsg(wxMpTemplateMessage);
  log.info("发送模板返回信息:"+wxTemplateResult);
} catch (Exception e) {
  log.error("发送模板消息异常:{}", e.getMessage());
  e.printStackTrace();
}

 以上步骤为常规步骤,其中的坑已经避开,常出现的问题是:

        1.Could not autowire. No beans of 'WxMpService' type found. -------> 无法自动接线。找不到“ WxMpService”类型的bean

        2.推送失败,可能是未将ip添加到微信公众平台白名单中

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356128.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号