1.1 公众号sdk配置

本文介绍了如何在Java项目中使用Weixin-Java-MP SDK进行微信公众号的配置,包括添加依赖、配置参数以及初始化WxMpService。通过示例代码展示了在Spring Boot应用中配置微信公众号的完整步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

系列文章目录

提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
例如:第一章 Python 机器学习入门之pandas的使用


一、weixin-java-mp 

一个很好用的微信操作sdk包,pom依赖。

<dependency>
    <groupId>com.github.binarywang</groupId>
    <artifactId>weixin-java-mp</artifactId>
    <version>4.1.0</version>
</dependency>

二、使用步骤

1. 配置参数

代码如下(示例):

application.yml配置参数

wxunion:
  appId: xxx
  secret: xxx
  token: 微信公众平台配置的,后续会用到

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "wxunion")
public class WxUnionParam {
    private String appId;
    private String secret;
    private String token;
}

2.配置服务类

import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
public class WxUnionConfig {

    @Autowired
    private WxUnionParam wxUnionParam;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Bean
    public WxMpService initWxMpService(){
        WxMpService  wxMpService = new WxMpServiceImpl();
        WxMpDefaultConfigImpl config = new WxMpRedisConfigImpl(new RedisTemplateWxRedisOps(stringRedisTemplate),"wx");
        config.setAppId(wxUnionParam.getAppId());
        config.setSecret(wxUnionParam.getSecret());
        config.setToken(wxUnionParam.getToken());
        wxMpService.setWxMpConfigStorage(config);
        return wxMpService;
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值