微信开发框架WxJava之微信公众号开发的入门使用篇

微信开发框架WxJava之微信公众号开发的入门使用篇

WxJava介绍

WxJava是一款基于Java语言的微信开发Java SDK,它提供了微信支付,开放平台,小程序,企业微信,公众号等多个平台的API接口,并将其封装为易于调用的Java方法,方便Java开发者快速开发与微信相关的应用。

GitHub地址:https://github.com/Wechat-Group/WxJava

使用WxJava只需要引入开发相关模块的maven依赖即可

com.github.binarywang (不同模块参考下文) 4.5.0微信小程序:weixin-java-miniapp微信支付:weixin-java-pay微信开放平台:weixin-java-open公众号(包括订阅号和服务号):weixin-java-mp企业号/企业微信:weixin-java-cp微信公众号

如果没有个人微信号或者企业微信号,可以申请测试公众号,并且拥有所有接口权限,推荐。

申请测试公众号

访问申请测试公众号,申请一个测试的微信公众号,测试微信公众号拥有所有高级功能。

测试公众号配置

申请测试公众号后,会分配一个测试的appid和秘钥,接着配置一个可用于内网穿透的地址

注意:URL地址指向本地开发能访问的某个接口

扫描测试号二维码以关注测试公众号,同时获取用户openid

WxJava微信公众号开发

WxJava对应的微信公众号开发文档

添加依赖

非Spring Boot:

com.github.binarywang weixin-java-mp 4.5.0

Spring Boot:

com.github.binarywang wx-java-mp-spring-boot-starter 4.5.0 配置微信参数

非Spring Boot方式引入依赖,需要自定义微信相关配置信息,同时需要初始化一个WxMpService实例。

# 自定义微信相关配置信息wx: # 消息模板ID templateId: o9YG7vWS8It-mddU2Wnknf1jgzTqZtLeBQRLhF54SXQ mp: # 微信公众号的appid appId: wxba7358c0c621200d # 信公众号的app secret secret: a0e9521e29a07e298ccba5b2c239958d # 微信公众号的toke token: token # 微信公众号的EncodingAESKey aesKey:

Spring Boot方式引入依赖,需要按约定进行微信相关配置,然后就可以直接进行相关开发。

具体配置参考:wx-java-mp-spring-boot-starter配置

# 公众号配置(必填)wx.mp.appId = appIdwx.mp.secret = @secretwx.mp.token = @tokenwx.mp.aesKey = @aesKey# 存储配置redis(可选)wx.mp.config-storage.type = Jedis # 配置类型: Memory(默认), Jedis, RedisTemplatewx.mp.config-storage.key-prefix = wx # 相关redis前缀配置: wx(默认)wx.mp.config-storage.redis.host = 127.0.0.1wx.mp.config-storage.redis.port = 6379#单机和sentinel同时存在时,优先使用sentinel配置#wx.mp.config-storage.redis.sentinel-ips=127.0.0.1:16379,127.0.0.1:26379#wx.mp.config-storage.redis.sentinel-name=mymaster# http客户端配置wx.mp.config-storage.http-client-type=httpclient # http客户端类型: HttpClient(默认), OkHttp, JoddHttpwx.mp.config-storage.http-proxy-host=wx.mp.config-storage.http-proxy-port=wx.mp.config-storage.http-proxy-username=wx.mp.config-storage.http-proxy-password=# 公众号地址host配置#wx.mp.hosts.api-host=http://proxy.com/#wx.mp.hosts.open-host=http://proxy.com/#wx.mp.hosts.mp-host=http://proxy.com/

wx-java-mp-spring-boot-starter主要自动配置了如下两个对象:

WxMpService:可以完成微信公众号提供的各种功能WxMpConfigStorage:保存了微信公众号配置信息实例化WxMpService

非Spring Boot方式引入依赖则需要自己实例化WxMpService对象。

1.创建WxMpProperties类,封装微信配置参数信息。

@Component@Data@ConfigurationProperties(prefix = "wx.mp")public class WxMpProperties { /** * 设置微信公众号的appid */ private String appId; /** * 设置微信公众号的app secret */ private String secret; /** * 设置微信公众号的token */ private String token; /** * 设置微信公众号的EncodingAESKey */ private String aesKey;}

2.创建WxMpConfiguration类,用于配置WxJava相关的实例对象。

@Configurationpublic class WxMpConfiguration { @Autowired private WxMpProperties wxMpProperties; /** * 微信客户端配置存储 */ @Bean public WxMpConfigStorage wxMpConfigStorage() { WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl(); // 设置微信公众号appId configStorage.setAppId(wxMpProperties.getAppId()); // 设置微信公众号appSecret configStorage.setSecret(wxMpProperties.getSecret()); // 设置微信公众号的token configStorage.setToken(wxMpProperties.getToken()); // 设置微信公众号的EncodingAESKey configStorage.setAesKey(wxMpProperties.getAesKey()); return configStorage; } /** * WxMpService多个实现类

比丘资源网 » 微信开发框架WxJava之微信公众号开发的入门使用篇

发表回复

提供最优质的资源集合

立即查看 了解详情