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