原文出处:
光闪
本框架基于SpringBoot实现,使用简单快捷,可以快速的完成一个微信公众号
在使用本框架前建议对微信公众号开发文档有所了解,不过在不了解公众号文档的情况下使用本框架也能完成一个简单的微信公众号。
一、简单示例:
1. 申请测试公众号
2. 准备完成,创建maven项目并加入maven依赖
<!-- 支持1.4.0.RELEASE及以上 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <dependencies> <!-- fastbootWeixin的核心依赖 --> <dependency> <groupId>com.mxixm</groupId> <artifactId>fastboot-weixin</artifactId> <version>0.1.2.alpha</version> </dependency> <!-- SpringBoot的web项目,必须 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 暂时只能使用apache的http,后续可加入其它http支持 --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcore</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> </dependencies>
3. 编写示例代码
在resource目录下新建配置文件application.properties或者其他spring boot支持的配置文件类型,加入配置:
- wx.token=随机生成的一串字母与数字,推荐使用随机生成32位的UUID
- wx.appid=测试号的appid,测试号管理界面有
- wx.appsecret=测试号的appsecret,测试号管理界面有
测试代码:
package com.mxixm.fastboot.weixin; import com.mxixm.fastboot.weixin.annotation.WxApplication; import com.mxixm.fastboot.weixin.annotation.WxAsyncMessage; import com.mxixm.fastboot.weixin.annotation.WxButton; import com.mxixm.fastboot.weixin.module.web.WxRequest; import com.mxixm.fastboot.weixin.module.event.WxEvent; import com.mxixm.fastboot.weixin.module.message.WxMessage; import com.mxixm.fastboot.weixin.module.user.WxUser; import com.mxixm.fastboot.weixin.mvc.annotation.WxController; import com.mxixm.fastboot.weixin.mvc.annotation.WxEventMapping; import com.mxixm.fastboot.weixin.mvc.annotation.WxMessageMapping; import org.springframework.boot.SpringApplication; @WxApplication @WxController public class WxApp { public static void main(String[] args) throws Exception { SpringApplication.run(WxApp.class, args); } /** * 定义微信菜单 */ @WxButton(group = WxButton.Group.LEFT, main = true, name = "左") public void left() { } /** * 定义微信菜单 */ @WxButton(group = WxButton.Group.RIGHT, main = true, name = "右") public void right() { } /** * 定义微信菜单,并接受事件 */ @WxButton(type = WxButton.Type.CLICK, group = WxButton.Group.LEFT, order = WxButton.Order.FIRST, name = "文本消息") public String leftFirst(WxRequest wxRequest, WxUser wxUser) { return "测试文本消息"; } /** * 定义微信菜单,并接受事件 */ @WxButton(type = WxButton.Type.VIEW, group = WxButton.Group.LEFT, order = WxButton.Order.SECOND, url = "http://baidu.com", name = "点击链接") @WxAsyncMessage public WxMessage link() { return WxMessage.News.builder().addItem("测试图文消息", "测试", "https://ss0.bdstatic.com/5aV1bjq