java对接微信

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

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

        1.引入jar

        <!-- WxJava公众号 -->
   <dependency>
           <groupId>com.github.binarywang</groupId>
           <artifactId>weixin-java-mp</artifactId>
   <version>3.6.0</version></dependency>

        2.配置公众号信息

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

        3.代码编写

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


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

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

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

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

    /**
     * 公众号aesKey
     */
    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;

/**
 * @ClassName WxConfig
 **/
@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;
    /**
     * 声明实例
     *
     * @return
     */
    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }


    /**
     * 微信客户端配置存储
     *
     * @return
     */
    @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<WxMpTemplateData> 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添加到微信公众平台白名单中

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值