Configuration of a single applet using weixin-java-miniapp configuration

Keywords: Java github SDK Maven

In the development of back-end interface for small programs, using weixin-java-miniapp module in weixin-java-tools can often achieve twice the result with half the effort.

Introducing weixin-java-tools

  • stay https://mvnrepository.com/ Search weixin-java-miniapp and enter the Wechat applet Java SDK project.
  • Select the appropriate official version for use.
  • Add the following configuration items to the dependency in maven:
<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-miniapp</artifactId>
    <version>3.3.0</version>
</dependency>
  • Add the following configuration items to gradle:
compile("com.github.binarywang:weixin-java-miniapp:3.3.0")
  • Note: The above version I used is 3.3.0, in fact, according to the version you want to use.

configuration file

  • The configuration file mainly configures four parameters, namely:

    • appId
    • secret
    • token
    • aesKey

Configuration initialization:

  • weixin-java-miniapp can be configured with annotations as follows:
  1. Create the WxMaConfiguration class in the config package.
  2. Use the @Configuration annotation to configure widget-related parameters, refer to the following code.
  3. This code example is a single widget configuration example. If you need to configure parameters of multiple widgets, please refer to the official case. Click to enter.
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;

@Configuration
public class WxMaConfiguration {

    // The way to get the configuration here can be changed to your own way, you can also get the configuration by annotations and so on.
    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;
    }
}

Start using

  • Where a widget-related interface is required, only the static method getWxMaService() in the configuration class is needed to obtain the wxMaService to start using, such as:
 // Get a widget service instance
WxMaService wxMaService = WxMaConfiguration.getWxMaService();
// Getting an Example of Small Program Two-Dimensional Code Generation
WxMaQrcodeService wxMaQrcodeService = wxMaService.getQrcodeService();
// Now you can start using wxMaQrcodeService for two-dimensional code correlation processing
....

Posted by scuzzo84 on Thu, 28 Mar 2019 21:48:29 -0700