在利用本框架前发起对微信公家号开拓文档有所相识,不外在不相识公家号文档的环境下利用本框架也能完成一个简朴的微信公家号。
一、简朴示例:
微信测试公家号申请链接
<!-- 支持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>
在resource目次下新建设置文件application.properties可能其他spring boot支持的设置文件范例,插手设置:
测试代码:
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/5aV1bjqh_Q23odCf/static/superman/img/logo/logo_white.png", "http://baidu.com").build(); } /** * 接管微信事件 * @param wxRequest * @param wxUser */ @WxEventMapping(type = WxEvent.Type.UNSUBSCRIBE) public void unsubscribe(WxRequest wxRequest, WxUser wxUser) { System.out.println(wxUser.getNickName() + "退订了公家号"); } /** * 接管用户文本动静,异步返回文本动静 * @param content * @return dummy */ @WxMessageMapping(type = WxMessage.Type.TEXT) @WxAsyncMessage public String text(WxRequest wxRequest, String content) { WxSession wxSession = wxRequest.getWxSession(); if (wxSession != null && wxSession.getAttribute("last") != null) { return "上次收到动静内容为" + wxSession.getAttribute("last"); } return "收到动静内容为" + content; } /** * 接管用户文本动静,同步返回图文动静 * @param content * @return dummy */ @WxMessageMapping(type = WxMessage.Type.TEXT, wildcard = "1*") public WxMessage message(WxSession wxSession, String content) { wxSession.setAttribute("last", content); return WxMessage.News.builder() .addItem(WxMessage.News.Item.builder().title(content).description("随便一点") .picUrl("http://k2.jsqq.net/uploads/allimg/1702/7_170225142233_1.png") .url("http://baidu.com").build()) .addItem(WxMessage.News.Item.builder().title("第二条").description("随便二点") .picUrl("http://k2.jsqq.net/uploads/allimg/1702/7_170225142233_1.png") .url("http://baidu.com").build()) .build(); } /** * 接管用户文本动静,异步返回文本动静 * @param content * @return dummy */ @WxMessageMapping(type = WxMessage.Type.TEXT, wildcard = "2*") @WxAsyncMessage public String text2(WxRequestBody.Text text, String content) { boolean match = text.getContent().equals(content); return "收到动静内容为" + content + "!功效匹配!" + match; } }
因为微信公家号需要设置本身的处事器接口,测试时可直接利用当地举办测试,利用内网穿透可以令微信公家平台会见到你本身当地的处事器。
软件可利用ngrok可能natapp,利用方法请参考两者官方文档。