作为一名新人程序员,可能写的不堪入目,但请轻喷,欢迎建议,作为一个野路子,肯定是有很多问题的。
使用jfinal jfinal-weixin 一个测试微信号做学习,数据库使用mysql,内网映射使用ngrok cn。
新建项目:web project
添加jar包:jfinal-2.2.jar、jfinal-weixin-1.7.jar、log4j、jackson、jetty、c3p0
配置web.xml
<filter> <filter-name>jfinal</filter-name> <filter-class>com.jfinal.core.JFinalFilter</filter-class> <init-param> <param-name>configClass</param-name> <param-value>tool.config.Config</param-value> </init-param> </filter> <filter-mapping> <filter-name>jfinal</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
配置数据库,因为是学习,暂时只是新建一份数据库,无表
create database if not exists wxtool default charset utf8 collate utf8_general_ci;
配置相关参数,在src目录下新建一个config.txt,内容:
jdbcUrl = jdbc:mysql://127.0.0.1/wxtool?autoReconnect=true&useSSL=false&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull user = root password = amin0125 devMode = true appId = wx71bfa64ba8e6c873 appSecret = d4624c36b6795d1d99dcf0547af5443d token = chmin
前三个数据库参数,devMode是是否打印相关开发信息,后三个是微信配置
新建包tool.config,新建类Config(参考demo)
public class Config extends JFinalConfig { @Override public void configConstant(Constants me) { PropKit.use("config.txt"); me.setDevMode(PropKit.getBoolean("devMode", false)); } @Override public void configRoute(Routes me) { } @Override public void configPlugin(Plugins me) { C3p0Plugin C3p0Plugin = createC3p0Plugin(); me.add(C3p0Plugin); ActiveRecordPlugin arp = new ActiveRecordPlugin(C3p0Plugin); me.add(arp); } @Override public void configInterceptor(Interceptors me) { } @Override public void configHandler(Handlers me) { } public static C3p0Plugin createC3p0Plugin() { return new C3p0Plugin(PropKit.get("jdbcUrl").trim(), PropKit.get("user").trim(), PropKit.get("password").trim()); } }
新建包 tool.controller,新建类 WxMsgCon,其实可以继承MsgControllerAdapter来减少需要实现的方法,本着学习的原因,直接继承了MsgController
public class WxMsgCon extends MsgController { @Override public ApiConfig getApiConfig() { // TODO Auto-generated method stub } @Override protected void processInTextMsg(InTextMsg inTextMsg) { // TODO Auto-generated method stub } @Override protected void processInImageMsg(InImageMsg inImageMsg) { // TODO Auto-generated method stub } @Override protected void processInVoiceMsg(InVoiceMsg inVoiceMsg) { // TODO Auto-generated method stub } @Override protected void processInVideoMsg(InVideoMsg inVideoMsg) { // TODO Auto-generated method stub } @Override protected void processInShortVideoMsg(InShortVideoMsg inShortVideoMsg) { // TODO Auto-generated method stub } @Override protected void processInLocationMsg(InLocationMsg inLocationMsg) { // TODO Auto-generated method stub } @Override protected void processInLinkMsg(InLinkMsg inLinkMsg) { TODO Auto-generated method stub } @Override protected void processInCustomEvent(InCustomEvent inCustomEvent) { // TODO Auto-generated method stub } @Override protected void processInFollowEvent(InFollowEvent inFollowEvent) { // TODO Auto-generated method stub } @Override protected void processInQrCodeEvent(InQrCodeEvent inQrCodeEvent) { // TODO Auto-generated method stub } @Override protected void processInLocationEvent(InLocationEvent inLocationEvent) { // TODO Auto-generated method stub } @Override protected void processInMassEvent(InMassEvent inMassEvent) { // TODO Auto-generated method stub } @Override protected void processInMenuEvent(InMenuEvent inMenuEvent) { // TODO Auto-generated method stub } @Override protected void processInSpeechRecognitionResults(InSpeechRecognitionResults inSpeechRecognitionResults) { // TODO Auto-generated method stub } @Override protected void processInTemplateMsgEvent(InTemplateMsgEvent inTemplateMsgEvent) { // TODO Auto-generated method stub } @Override protected void processInShakearoundUserShakeEvent(InShakearoundUserShakeEvent inShakearoundUserShakeEvent) { // TODO Auto-generated method stub } @Override protected void processInVerifySuccessEvent(InVerifySuccessEvent inVerifySuccessEvent) { // TODO Auto-generated method stub } @Override protected void processInVerifyFailEvent(InVerifyFailEvent inVerifyFailEvent) { // TODO Auto-generated method stub } @Override protected void processInPoiCheckNotifyEvent(InPoiCheckNotifyEvent inPoiCheckNotifyEvent) { // TODO Auto-generated method stub } }
在getApiConfig中配置一下微信参数
@Override public ApiConfig getApiConfig() { ApiConfig ac = new ApiConfig(); ac.setToken(PropKit.get("token")); ac.setAppId(PropKit.get("appId")); ac.setAppSecret(PropKit.get("appSecret")); return ac; }
新建包tool.service,新建类TextMsgService
public class TextMsgService { public static TextMsgService me = new TextMsgService(); public OutMsg mappingKeyword(InTextMsg inTextMsg) { OutTextMsg outTextMsg = new OutTextMsg(inTextMsg); outTextMsg.setContent("你输入的文字信息是:" + inTextMsg.getContent()); return outTextMsg; } }
在tool.controller.WxMsgCon的processInTextMsg方法中添加如下方法:
protected void processInTextMsg(InTextMsg inTextMsg) { render(TextMsgService.me.mappingKeyword(inTextMsg)); }
打开tool.config报下的Config类的configRoute方法,添加一行
me.add("/wxMsg", WxMsgCon.class);
右键项目,选择debug As Java Application启动,输入jfinal,启动,看到控制台如下输出
Starting JFinal 2.2 Starting scanner at interval of 5 seconds. Starting web server on port: 80 Starting Complete. Welcome To The JFinal World :)
启动ngrok
ngrok -config ngrok.cfg -subdomain chmin 80
登陆微信公众平台,配置URL和token
URL http://chmin.ngrok.natapp.cn/wxMsg Token chmin
打开微信,关注公众号,发送一条文字信息,效果如图:
说明配置成功。