在進行小程序后端接口開發方面,使用weixin-java-tools中的weixin-java-miniapp模塊,往往可以事半功倍。
引入weixin-java-tools
在https://mvnrepository.com/中搜索weixin-java-miniapp,進入微信小程序 Java SDK這個項目中。
選擇相應正式版本來進行使用。
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,實際中根據你要使用的版本來用。
配置文件
配置文件中主要配置四項參數,分別是:
appId secret token aesKey配置初始化:
weixin-java-miniapp可以使用注解來進行配置,具體步驟如下:
在config包中創建WxMaConfiguration類。
使用@Configuration注解來進行小程序相關的參數配置,可參考以下代碼。
該代碼示例中是單個小程序配置示例,如果需要配置多個小程序的參數,請參考官方案例點擊進入。
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; }}開始使用
在需要使用小程序相關接口的地方,只需要通過該配置類中的靜態方法getWxMaService()來獲取到wxMaService即可開始使用,如:
// 獲取小程序服務實例WxMaService wxMaService = WxMaConfiguration.getWxMaService();// 獲取小程序二維碼生成實例WxMaQrcodeService wxMaQrcodeService = wxMaService.getQrcodeService();// 便可以開始使用wxMaQrcodeService來進行二維碼相關的處理了....
新聞熱點
疑難解答
圖片精選