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); //构建消息格式 ListlistData = 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添加到微信公众平台白名单中



