微信公众号开发体验接口 Bertha 。 2022-04-13 05:43 154阅读 0赞 1.新建Module wechat ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plYWw5cw_size_16_color_FFFFFF_t_70] 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 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plYWw5cw_size_16_color_FFFFFF_t_70 1] ![在这里插入图片描述][20181127182313922.png] 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 ![在这里插入图片描述][20181127182455398.png] 打开redis的服务,输入命令查询 ![在这里插入图片描述][20181127182820401.png] 如果能够查询到该键,说明键已经存入 项目结构图 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plYWw5cw_size_16_color_FFFFFF_t_70 2] 容易报错的地方: ①配置文件没有修改本机的ipconfig,不能查询到access\_token ②jar包下载有问题,工厂不能实例化某些对象 ③ ![在这里插入图片描述][20181127182916574.png] 修改URL重新提交时,如若配置失败,可能时natapp的穿透网络的问题,不影响访问 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plYWw5cw_size_16_color_FFFFFF_t_70 3] 关注公众号的样本 ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plYWw5cw_size_16_color_FFFFFF_t_70 4] ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plYWw5cw_size_16_color_FFFFFF_t_70 5] 案例的完整代码:[https://download.csdn.net/download/zeal9s/10811324][https_download.csdn.net_download_zeal9s_10811324] **说在最后的话:编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~** [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plYWw5cw_size_16_color_FFFFFF_t_70]: /images/20220413/1aaf0fd3454742cbb1ec6ade6f27d095.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plYWw5cw_size_16_color_FFFFFF_t_70 1]: /images/20220413/2e1efa3f738d4e00897d3ff7a2846424.png [20181127182313922.png]: /images/20220413/83b91d86f3a34a0a8813ce0076443d78.png [20181127182455398.png]: /images/20220413/f5440ddf02004675981ee2c9c3fe64a0.png [20181127182820401.png]: /images/20220413/22f7e67ed1634b008c0a39674029ba77.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plYWw5cw_size_16_color_FFFFFF_t_70 2]: /images/20220413/aacda022681140a5babb58a7e8b8285e.png [20181127182916574.png]: /images/20220413/dbd162ed39a84bc1bef979bf8bed0020.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plYWw5cw_size_16_color_FFFFFF_t_70 3]: /images/20220413/41bf172b00494bf193c6e04e69df7bd7.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plYWw5cw_size_16_color_FFFFFF_t_70 4]: /images/20220413/0df9bc8598374a228fca4552ebbb0d12.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3plYWw5cw_size_16_color_FFFFFF_t_70 5]: /images/20220413/55f2108f81e24bbab331f8c810925b27.png [https_download.csdn.net_download_zeal9s_10811324]: https://download.csdn.net/download/zeal9s/10811324
还没有评论,来说两句吧...