微信公众号开发体验接口

1.新建Module wechat
微信公众号开发体验接口
2.按照上篇文章,先成功接入微信接口
3.由于winxin4j使用redis进行缓存Token,所以请先配置springboot+redis的集成,它的access_token每2小时变换一次。

1)启动redis,连接radis
2)配置项目中的redis信息

导入依赖到pom.xml文件

	 <!--引入redis的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

新建radis包,和RedisConfig.java类

package com.springboot.wechat.redis;


import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * redis配置类
 **/
@Configuration
@EnableCaching//开启注解式缓存
//继承CachingConfigurerSupport,为了自定义生成KEY的策略。可以不继承。
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
        StringRedisTemplate stringRedisTemplate = new StringRedisTemplate();
        stringRedisTemplate.setConnectionFactory(factory);
        return stringRedisTemplate;
    }

}

weixin4j.properties

#微信SDK配置文件
#读取规则:优先读取System.getProperty()
#再从weixin4j.properties读取,key
#如果System.getProperty()与weixin4j.properties都没设置,则默认未NULL

#开发者调试设置
weixin4j.debug=true
#公众号Token
weixin4j.token=myToken

#公众号原始ID
weixin4j.oauth.originalid=gh_da433f9665e5
#开发者第三方用户唯一凭证
weixin4j.oauth.appid=wx2086f4cf7d2a0274
#开发者第三方用户唯一凭证**
weixin4j.oauth.secret=57f0ea52a5fdb7a93c5babc2a452d438


#消息加密方式 0:明文模式(默认), 1:兼容模式, 2:安全模式(推荐)
weixin4j.oauth.encodingtype=0
#消息加***(43位字符组成A-Za-z0-9)
weixin4j.oauth.encodingaeskey=0123456789abcedfghijklmnopqrstuvwxyzZXCVBNM
#网页安全授权URL
weixin4j.oauth.url=

#公众平台接口域名
#通用域名(api.weixin.qq.com),使用该域名将访问官方指定就近的接入点;
#上海域名(sh.api.weixin.qq.com),使用该域名将访问上海的接入点;
#深圳域名(sz.api.weixin.qq.com),使用该域名将访问深圳的接入点;
#香港域名(hk.api.weixin.qq.com),使用该域名将访问香港的接入点。
weixin4j.api.domain=api.weixin.qq.com

#微信支付_商户ID
weixin4j.pay.partner.id=
#微信支付_商户**
weixin4j.pay.partner.key=
#微信支付_通知URL
weixin4j.pay.notify_url=

#连接超时设置
weixin4j.http.connectionTimeout=25000
#请求超时设置
weixin4j.http.readTimeout=25000
#证书路径
weixin4j.http.cert.path=
weixin4j.http.cert.secret=

#默认消息处理函数
weixin4j.handler=org.weixin4j.spi.DefaultMessageHandler
#weixin4j.message.handler.normal=org.weixin4j.spi.DefaultNormalMessageHandler
#weixin4j.message.handler.event=org.weixin4j.spi.DefaultEventMessageHandler

weixin4j.message.handler.normal=com.springboot.wechat.weixin.MyAtsNormalMessageHandler
weixin4j.message.handler.event=com.springboot.wechat.weixin.MyAtsEventMessageHandler


微信公众号开发体验接口
微信公众号开发体验接口
application.properties

server.port=80



spring.datasource.username=root
spring.datasource.password=
spring.datasource.url=jdbc:mysql://localhost:3306/quartz?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver


#指定HttpServletRequest的属性是否可以覆盖controller的model的同名项
spring.freemarker.allow-request-override=false
#req访问request
spring.freemarker.request-context-attribute=req
#后缀名freemarker默认后缀为.ftl,当然你也可以改成自己习惯的.html
spring.freemarker.suffix=.ftl
#设置响应的内容类型
spring.freemarker.content-type=text/html;charset=utf-8
#是否允许mvc使用freemarker
spring.freemarker.enabled=true
#是否开启template caching
spring.freemarker.cache=false
#设定模板的加载路径,多个以逗号分隔,默认: [“classpath:/templates/”]
spring.freemarker.template-loader-path=classpath:/templates/
#设定Template的编码
spring.freemarker.charset=UTF-8
#数字格式化,无小数点
spring.freemarker.number_format=0.##


# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=192.168.110.128
# Redis服务器连接端口
spring.redis.port=6379
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=5000

微信公众号开发体验接口
打开redis的服务,输入命令查询
微信公众号开发体验接口
如果能够查询到该键,说明键已经存入

项目结构图
微信公众号开发体验接口

容易报错的地方:
①配置文件没有修改本机的ipconfig,不能查询到access_token
②jar包下载有问题,工厂不能实例化某些对象

微信公众号开发体验接口
修改URL重新提交时,如若配置失败,可能时natapp的穿透网络的问题,不影响访问
微信公众号开发体验接口
关注公众号的样本
微信公众号开发体验接口
微信公众号开发体验接口
案例的完整代码:https://download.csdn.net/download/zeal9s/10811324
说在最后的话:编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~