【微信公众号】SpringBoot集成微信公众号

一、前置工作

1、注册公众号

注册地址:https://mp.weixin.qq.com/cgi-bin/registermidpage?action=index&lang=zh_CN

注册流程参考:https://kf.qq.com/touch/faq/150804UVr222150804quq6B7.html?platform=15
在这里插入图片描述

2、获取appId和appSecret核心参数

在这里插入图片描述
在这里插入图片描述

二、SpringBoot集成微信公众号

1、引入pom依赖

        <!-- 公众号 -->
        <dependency>
            <groupId>com.github.binarywang</groupId>
            <artifactId>wx-java-mp-spring-boot-starter</artifactId>
            <version>4.5.0</version>
        </dependency>

2、yml配置


wechat:
  mp:
    appId: xxx #公众号appId
    secret: xxx #公众号秘钥
    token:
    url:
    aesKey:

3、java代码文件

3.1、Properties 配置类


import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * Author tm
 * Version 1.0
 */
@Configuration
@ConfigurationProperties(prefix = "wechat.mp")
@Data
public class WechatMpProperties {
    /**
     * 设置微信公众号的appid
     */
    private String appId;

    /**
     * 设置微信公众号的app secret
     */
    private String secret;

    /**
     * 设置微信公众号的token
     */
    private String token;

    /**
     * 设置微信公众号的EncodingAESKey
     */
    private String aesKey;
}


3.2 Configuration 服务类



import lombok.AllArgsConstructor;
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.jeecg.modules.fag.properties.WechatMpProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Author tm
 * Version 1.0
 */
@AllArgsConstructor
@Configuration
public class WxMpConfiguration {

    @Resource
    private WechatMpProperties properties;

    @Bean
    public WxMpService wxMpService() {
        WxMpService service = new WxMpServiceImpl();
        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
        configStorage.setAppId(properties.getAppId());
        configStorage.setSecret(properties.getSecret());
        configStorage.setToken(properties.getToken());
        configStorage.setAesKey(properties.getAesKey());
        Map<String, WxMpConfigStorage> storageMap = new HashMap<>();
        storageMap.put(properties.getAppId(), configStorage);
        service.setMultiConfigStorages(storageMap);
        return service;
    }

}

4、使用示例

4.1、获取公众号发布的文章列表


import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpFreePublishService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.freepublish.WxMpFreePublishList;
import org.jeecg.common.api.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Author tm
 * Version 1.0
 */
@Slf4j
@Service
public class ArticleServiceImpl {


    @Resource
    private WxMpService wxMpService;

    public Result articleList(int size, int current) {

        WxMpFreePublishService freePublishService = wxMpService.getFreePublishService();

        int offset = (size - 1) * 10;


        WxMpFreePublishList publicationRecords = null;
        try {
            publicationRecords = freePublishService.getPublicationRecords(offset, current, 1);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }

        return Result.ok("查询成功", publicationRecords);
    }


}


5、更多接口使用请参考

me.chanjar.weixin.mp.api.WxMpService
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值