引言
在上一节《淘东电商项目(12) -搭建企业级微信公众号》,主要讲解如何搭建微信公众号,以及WxJava 案例的使用。
代码已提交至Github(版本号:
7f8e64fdfccf846b89e71dc2e32c1d4988b8f91e
),有兴趣的同学可以下载来看看:https://github.com/ylw-github/taodong-shop
本文开始讲解如何把WxJava框架整合到我们的项目。
本文目录结构:
l____引言
l____ 1. 微信公众号开发原理
l____ 2.测试
l________ 2.1测试接口配置信息
l________ 2.2发送消息测试
l____总结
1. 微信公众号开发原理
在上一篇博客,我们从使用了《weixin-java-mp-demo-springboot》案例来调用了测试了微信公众号,现在我们要整合到我们的项目,步骤如下:
step1:微信服务模块(taodong-shop-service-weixin
)引入Maven依赖:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.6.0</version>
</dependency>
step2:配合application.yml,直接从上一篇博客的配置文件复制过来,我是配置到Apollo分布式配置中心的(不懂的可以参考我之前的博客《淘东电商项目(10) -Apollo分布式配置中心管理application.yml》):
wx:
mp:
configs:
- appId: wx???95 #(一个公众号的appid)
secret: bb42????1c3 #(公众号的appsecret)
token: ylw666 #(接口配置里的Token值)
#aesKey: 111(接口配置里的EncodingAESKey值)
step3:直接复制《weixin-java-mp-demo-springboot》案例的这几个目录到微信服务模块。
step4 :启动项目,接下来进行测试
2. 测试
2.1 测试接口配置信息
启动项目,微信公众号里点击提交:
可以看到配置成功。
2.1 发送消息测试
平台会报错,报错内容为:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoSuchMethodError: com.thoughtworks.xstream.XStream.setupDefaultSecurity(Lcom/thoughtworks/xstream/XStream;)V] with root cause
原因是微信框架引入了xstream的版本为1.4.1 而springCloud中eureka-client也引入了xstream为了1.4.9从而版本有冲突。修改依赖的代码:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.6.0</version>
<exclusions>
<exclusion>
<artifactId>xstream</artifactId>
<groupId>com.thoughtworks.xstream</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>
spring-cloud-starter-netflix-eureka-client
</artifactId>
<exclusions>
<exclusion>
<artifactId>xstream</artifactId>
<groupId>com.thoughtworks.xstream</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<artifactId>xstream</artifactId>
<groupId>com.thoughtworks.xstream</groupId>
<version>1.4.10</version>
</dependency>
再次发送消息,可以消息发送成功,有回复。
总结
注意:导入我的项目时,可能报错,但是却正常运行,这是由于项目使用了Lombok插件,需要先在IDEA里添加Lombok插件,具体的自己百度!