在進(jìn)行小程序后端接口開發(fā)方面,使用weixin-java-tools中的weixin-java-miniapp模塊,往往可以事半功倍。
引入weixin-java-tools
在https://mvnrepository.com/中搜索weixin-java-miniapp,進(jìn)入微信小程序 Java SDK這個項目中。
選擇相應(yīng)正式版本來進(jìn)行使用。
maven中在依賴中添加如下配置項:
<dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-miniapp</artifactId> <version>3.3.0</version></dependency>
gradle中添加如下配置項:
compile("com.github.binarywang:weixin-java-miniapp:3.3.0")注意:以上我用的版本是3.3.0,實(shí)際中根據(jù)你要使用的版本來用。
配置文件
配置文件中主要配置四項參數(shù),分別是:
配置初始化:
weixin-java-miniapp可以使用注解來進(jìn)行配置,具體步驟如下:
在config包中創(chuàng)建WxMaConfiguration類。
使用@Configuration注解來進(jìn)行小程序相關(guān)的參數(shù)配置,可參考以下代碼。
該代碼示例中是單個小程序配置示例,如果需要配置多個小程序的參數(shù),請參考官方案例點(diǎn)擊進(jìn)入。
package com.diboot.miniapp.config;import cn.binarywang.wx.miniapp.api.WxMaService;import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;import cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;import dibo.framework.config.BaseConfig;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;@Configurationpublic class WxMaConfiguration { // 此處獲取配置的方式可以改成你自己的方式,也可以注解等方式獲取配置等。 private static final String appId = BaseConfig.getProperty("wechat.appId"); private static final String secret = BaseConfig.getProperty("wechat.secret"); private static final String token = BaseConfig.getProperty("wechat.token"); private static final String aesKey = BaseConfig.getProperty("wechat.aesKey"); private static WxMaService wxMaService = null; @Bean public Object services(){ WxMaInMemoryConfig config = new WxMaInMemoryConfig(); config.setAppid(appId); config.setSecret(secret); config.setToken(token); config.setAesKey(aesKey); wxMaService = new WxMaServiceImpl(); wxMaService.setWxMaConfig(config); return Boolean.TRUE; } public static WxMaService getWxMaService(){ return wxMaService; }}開始使用
在需要使用小程序相關(guān)接口的地方,只需要通過該配置類中的靜態(tài)方法getWxMaService()來獲取到wxMaService即可開始使用,如:
// 獲取小程序服務(wù)實(shí)例WxMaService wxMaService = WxMaConfiguration.getWxMaService();// 獲取小程序二維碼生成實(shí)例WxMaQrcodeService wxMaQrcodeService = wxMaService.getQrcodeService();// 便可以開始使用wxMaQrcodeService來進(jìn)行二維碼相關(guān)的處理了....
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答