SpringBoot对接微信公众号,持续更新

一、创建SpringBoot项目,配置内网穿透

1. 创建一个SpringBoot项目,此步骤省略

2. 在pom中引用班纳睿工具包

3. 内网穿透配置

我这边用到的是

在ngrok中选择免费的项目,然后配置相对应的域名映射到本地的相应端口

配置完后,在隧道管理菜单可以看到隧道 id

下载ngrok的客户端,我这里下载的是 windows版本,下载解压后,打开 windows_amd64文件夹,点击Sunny-Ngrok启动工具,然后输入隧道id敲回车。界面出现oline表示穿透成功。

二、 在微信公众平台申请测试公众号,对接SpringBoot项目

申请测试号后,可以得到appID,appsecret

在SpringBoot项目中创建config文件夹

在 config文件夹下创建 WechatMpProperties类,获取yml文件中的配置信息

在 config文件下创建 WxMpConfiguration类,将 WechatMpProperties的配置信息配置进去

@AllArgsConstructor
@Configuration
public class WxMpConfiguration {

    @Autowired
    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;
    }

}

在 SpringBoot项目中创建 controller文件夹,在 controller文件夹下创建 WxPortalController类

在接口配置信息,url 填写 域名+ /wx/portal/{appid}

配置完后,在下发点击发送按钮。配置成功,会弹出配置成功窗口。

三、 开发关注公众号和取关公众号时相应的业务

经验分享 程序员 微信小程序 职场和发展