新增weixin4j-example示例工程
This commit is contained in:
+90
@@ -0,0 +1,90 @@
|
||||
package com.foximy.weixin4j.example.server;
|
||||
|
||||
import io.netty.util.internal.logging.InternalLoggerFactory;
|
||||
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.spring.SpringBeanFactory;
|
||||
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
|
||||
import com.foxinmy.weixin4j.util.AesToken;
|
||||
|
||||
/**
|
||||
* 微信消息服务:需要另外开启一个线程去启动服务,比如在spring mvc中
|
||||
*
|
||||
* @className Weixin4jServerStartupWithThread
|
||||
* @author jy
|
||||
* @date 2015年5月7日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class Weixin4jServerStartupWithThread implements ApplicationContextAware {
|
||||
/**
|
||||
* 服务监听的端口号,目前微信只支持80端口,可以考虑用nginx做转发到此端口
|
||||
*/
|
||||
private final int port;
|
||||
/**
|
||||
* 服务器token信息
|
||||
*/
|
||||
/**
|
||||
* 明文模式:String aesToken = ""; 密文模式:AesToken aesToken = new
|
||||
* AesToken("公众号appid", "公众号token","公众号加密/解密消息的密钥");
|
||||
*/
|
||||
private final AesToken aesToken;
|
||||
/**
|
||||
* 处理微信消息的全限包名(也可通过addHandler方式一个一个添加)
|
||||
*/
|
||||
private final String handlerPackage;
|
||||
/**
|
||||
* 用spring去获取bean
|
||||
*/
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
private Weixin4jServerStartupWithThread(int port, AesToken aesToken,
|
||||
String handlerPackage) {
|
||||
this.port = port;
|
||||
this.aesToken = aesToken;
|
||||
this.handlerPackage = handlerPackage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext)
|
||||
throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
private ExecutorService executor;
|
||||
|
||||
/**
|
||||
* 启动函数
|
||||
*
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public void start() {
|
||||
executor = Executors.newCachedThreadPool();
|
||||
executor.execute(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
new WeixinServerBootstrap(aesToken)
|
||||
.handlerPackagesToScan(handlerPackage)
|
||||
.resolveBeanFactory(
|
||||
new SpringBeanFactory(applicationContext))
|
||||
.openAlwaysResponse().startup(port);
|
||||
} catch (WeixinException e) {
|
||||
InternalLoggerFactory.getInstance(getClass()).error(
|
||||
"weixin4j server startup:FAIL", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
executor.shutdown();
|
||||
}
|
||||
}
|
||||
+52
@@ -0,0 +1,52 @@
|
||||
package com.foximy.weixin4j.example.server;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.spring.SpringBeanFactory;
|
||||
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
|
||||
|
||||
/**
|
||||
* 微信消息服务:单独作为一个服务jar包启动
|
||||
*
|
||||
* @className Weixin4jServerStartupWithoutThread
|
||||
* @author jy
|
||||
* @date 2015年5月7日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class Weixin4jServerStartupWithoutThread {
|
||||
/**
|
||||
* 服务监听的端口号,目前微信只支持80端口,可以考虑用nginx做转发到此端口
|
||||
*/
|
||||
private static int port = 10000;
|
||||
/**
|
||||
* 服务器token信息
|
||||
*/
|
||||
/**
|
||||
* 明文模式:String aesToken = ""; 密文模式:AesToken aesToken = new
|
||||
* AesToken("公众号appid", "公众号token","公众号加密/解密消息的密钥");
|
||||
*/
|
||||
private static String aesToken = "weixin4j";
|
||||
/**
|
||||
* 处理微信消息的全限包名(也可通过addHandler方式一个一个添加)
|
||||
*/
|
||||
private static String handlerPackage = "com.foximy.weixin4j.example.server.handler";
|
||||
|
||||
/**
|
||||
* 入口函数 可使用assembly插件打成可执行zip包:https://github.com/foxinmy/weixin4j/wiki/
|
||||
* assembly%E6%89%93%E5%8C%85
|
||||
*
|
||||
* @param args
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public static void main(String[] args) throws WeixinException {
|
||||
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
|
||||
new String[] { "classpath:/spring-bean.xml" });
|
||||
new WeixinServerBootstrap(aesToken)
|
||||
.handlerPackagesToScan(handlerPackage).openAlwaysResponse()
|
||||
.resolveBeanFactory(new SpringBeanFactory(applicationContext))
|
||||
.startup(port);
|
||||
}
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.foximy.weixin4j.example.server.handler;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
|
||||
import com.foxinmy.weixin4j.message.TextMessage;
|
||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
import com.foxinmy.weixin4j.response.TextResponse;
|
||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
|
||||
/**
|
||||
* 输入 hello 回复 world
|
||||
*
|
||||
* @className HelloMessageHandler
|
||||
* @author jy
|
||||
* @date 2015年12月27日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class HelloMessageHandler extends MessageHandlerAdapter<TextMessage> {
|
||||
@Override
|
||||
public boolean canHandle0(WeixinRequest request, TextMessage message)
|
||||
throws WeixinException {
|
||||
return message.getContent().equalsIgnoreCase("hello");
|
||||
}
|
||||
|
||||
@Override
|
||||
public WeixinResponse doHandle0(WeixinRequest request, TextMessage message)
|
||||
throws WeixinException {
|
||||
/**
|
||||
* 返回用户「world」文本
|
||||
*/
|
||||
return new TextResponse("world");
|
||||
}
|
||||
|
||||
/**
|
||||
* 提高权重 > TextMessageHandler
|
||||
*/
|
||||
@Override
|
||||
public int weight() {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.foximy.weixin4j.example.server.handler;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
|
||||
import com.foxinmy.weixin4j.mp.event.ScribeEventMessage;
|
||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
import com.foxinmy.weixin4j.response.TextResponse;
|
||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
|
||||
/**
|
||||
* 处理关注消息
|
||||
*
|
||||
* @className SubscribeMessageHandler
|
||||
* @author jy
|
||||
* @date 2015年12月3日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class SubscribeMessageHandler extends
|
||||
MessageHandlerAdapter<ScribeEventMessage> {
|
||||
|
||||
@Override
|
||||
public WeixinResponse doHandle0(WeixinRequest arg0, ScribeEventMessage arg1)
|
||||
throws WeixinException {
|
||||
return new TextResponse("欢迎关注~");
|
||||
}
|
||||
}
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.foximy.weixin4j.example.server.handler;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
|
||||
import com.foxinmy.weixin4j.message.TextMessage;
|
||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
import com.foxinmy.weixin4j.response.TextResponse;
|
||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
|
||||
/**
|
||||
* 文本消息处理
|
||||
*
|
||||
* @className TextMessageHandler
|
||||
* @author jy
|
||||
* @date 2015年11月18日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class TextMessageHandler extends MessageHandlerAdapter<TextMessage> {
|
||||
|
||||
@Override
|
||||
public WeixinResponse doHandle0(WeixinRequest request, TextMessage message)
|
||||
throws WeixinException {
|
||||
return new TextResponse("收到了文本消息");
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.foximy.weixin4j.example.server.handler;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
|
||||
import com.foxinmy.weixin4j.message.VoiceMessage;
|
||||
import com.foxinmy.weixin4j.request.WeixinRequest;
|
||||
import com.foxinmy.weixin4j.response.TextResponse;
|
||||
import com.foxinmy.weixin4j.response.WeixinResponse;
|
||||
|
||||
/**
|
||||
* 只处理语音消息
|
||||
*
|
||||
* @className VoiceMessageHandler
|
||||
* @author jy
|
||||
* @date 2015年11月18日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class VoiceMessageHandler extends MessageHandlerAdapter<VoiceMessage> {
|
||||
|
||||
@Override
|
||||
public WeixinResponse doHandle0(WeixinRequest request, VoiceMessage message)
|
||||
throws WeixinException {
|
||||
/**
|
||||
* 返回一段文字给用户
|
||||
*/
|
||||
return new TextResponse("你讲了一句话");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
|
||||
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
|
||||
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
|
||||
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
|
||||
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
|
||||
|
||||
<!-- 微信接口代理~start -->
|
||||
<bean id="weixinProxy" class="com.foxinmy.weixin4j.mp.WeixinProxy">
|
||||
<constructor-arg>
|
||||
<bean class="com.foxinmy.weixin4j.util.Weixin4jSettings">
|
||||
<!-- 公众号信息:不声明则默认使用weixin4j.properties配置的weixin4j.account字段 -->
|
||||
<constructor-arg>
|
||||
<bean class="com.foxinmy.weixin4j.model.WeixinAccount">
|
||||
<constructor-arg index="0" value="公众账号的appid" />
|
||||
<constructor-arg index="1" value="公众账号的appsecret" />
|
||||
</bean>
|
||||
</constructor-arg>
|
||||
<!-- token存储:不声明则默认使用文件方式(FileTokenStorager)保存 -->
|
||||
<property name="tokenStorager">
|
||||
<!-- redis保存token -->
|
||||
<bean class="com.foxinmy.weixin4j.token.RedisTokenStorager">
|
||||
<constructor-arg type="redis.clients.jedis.JedisPool">
|
||||
<bean class="redis.clients.jedis.JedisPool">
|
||||
<constructor-arg index="0">
|
||||
<bean class="redis.clients.jedis.JedisPoolConfig">
|
||||
<property name="maxTotal" value="${redis.maxTotal}" />
|
||||
<property name="maxIdle" value="${redis.maxIdle}" />
|
||||
<property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
|
||||
<property name="testOnBorrow" value="${redis.testOnBorrow}" />
|
||||
</bean>
|
||||
</constructor-arg>
|
||||
<constructor-arg index="1" value="${redis.host}" />
|
||||
<constructor-arg index="2" value="${redis.port}" />
|
||||
<constructor-arg index="3" value="${redis.timeout}" />
|
||||
</bean>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
</property>
|
||||
<!-- http参数:http请求时的参数,比如代理、超时等配置信息,暂时还未实现 -->
|
||||
<property name="httpParams">
|
||||
<bean class="com.foxinmy.weixin4j.http.HttpParams" />
|
||||
</property>
|
||||
<!-- 临时目录:weixin4j调用某些接口时需要用到的临时目录,不声明则获取顺序为:weixin4j.properties#weixin4j.tmpdir->`java.io.tmp` -->
|
||||
<property name="tmpdir" value="/tmp/weixin4j" />
|
||||
</bean>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
<!-- 微信接口代理~end -->
|
||||
|
||||
<!-- 微信支付接口代理~start -->
|
||||
<bean id="weixinPayProxy" class="com.foxinmy.weixin4j.payment.WeixinPayProxy">
|
||||
<constructor-arg>
|
||||
<bean class="com.foxinmy.weixin4j.util.Weixin4jSettings">
|
||||
<!-- 商户信息:不声明则默认使用weixin4j.properties配置的weixin4j.account字段 -->
|
||||
<constructor-arg>
|
||||
<bean class="com.foxinmy.weixin4j.model.WeixinPayAccount">
|
||||
<constructor-arg index="0" value="公众账号的appid" />
|
||||
<constructor-arg index="1" value="商户平台的支付密钥:paySignkey" />
|
||||
<constructor-arg index="2" value="商户平台的商户ID:mchId" />
|
||||
</bean>
|
||||
</constructor-arg>
|
||||
<!-- ca证书:调用某些支付接口(如退款、红包)需要用到的证书文件,不声明则获取顺序为:weixin4j.properties#weixin4j.certificate.file->`classpath:ca.p12` -->
|
||||
<property name="certificateFile" value="/path/to/certificate/file" />
|
||||
<!-- http参数:http请求时的参数,比如代理、超时等配置信息,暂时还未实现 -->
|
||||
<property name="httpParams">
|
||||
<bean class="com.foxinmy.weixin4j.http.HttpParams" />
|
||||
</property>
|
||||
<!-- 临时目录:weixin4j调用某些接口时需要用到的临时目录,不声明则获取顺序为:weixin4j.properties#weixin4j.tmpdir->`java.io.tmp` -->
|
||||
<property name="tmpdir" value="/tmp/weixin4j" />
|
||||
</bean>
|
||||
</constructor-arg>
|
||||
</bean>
|
||||
<!-- 微信支付接口代理~end -->
|
||||
|
||||
<!-- 微信消息服务~start -->
|
||||
<bean
|
||||
class="com.foximy.weixin4j.example.server.Weixin4jServerStartupWithThread"
|
||||
init-method="start" destroy-method="stop">
|
||||
<!-- 端口号 微信暂时只支持80端口 所以需要自己把微信被动消息请求转发(nginx等)到这个端口上来 -->
|
||||
<constructor-arg index="0" value="10000" />
|
||||
<!-- token信息 -->
|
||||
<constructor-arg index="1">
|
||||
<!-- 明文模式 -->
|
||||
<bean class="com.foxinmy.weixin4j.util.AesToken">
|
||||
<constructor-arg index="0" value="weixin4j" />
|
||||
</bean>
|
||||
<!-- 加密模式 -->
|
||||
<!-- bean class="com.foxinmy.weixin4j.util.AesToken"> <constructor-arg
|
||||
index="0" value="公众号的应用ID(appid/corpid)" /> <constructor-arg index="1" value="开发者Token"
|
||||
/> <constructor-arg index="2" value="解密的EncodingAESKey" /> </bean -->
|
||||
</constructor-arg>
|
||||
<!-- 处理微信消息的全限包名 -->
|
||||
<constructor-arg index="2"
|
||||
value="com.foximy.weixin4j.example.server.handler" />
|
||||
</bean>
|
||||
<!-- 微信消息服务~end -->
|
||||
</beans>
|
||||
@@ -0,0 +1,18 @@
|
||||
# \u516c\u4f17\u53f7\u4fe1\u606f \u8bf7\u6309\u9700\u586b\u5199
|
||||
weixin4j.account={"id":"wx4ab8f8de58159a57","secret":"1d4eb0f4bf556aaed539f30ed05ca795",\
|
||||
"mchId":"\u5fae\u4fe1\u5546\u6237\u53f7 \u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165",\
|
||||
"certificateKey":"\u52a0\u8f7d\u652f\u4ed8\u8bc1\u4e66\u6587\u4ef6\u7684\u5bc6\u7801 \u5982\u679c\u4e0d\u586b\u5199\u5219\u9ed8\u8ba4\u83b7\u53d6mchId\u4f5c\u4e3a\u5bc6\u7801",\
|
||||
"paySignKey":"\u5fae\u4fe1\u652f\u4ed8\u4e2d\u8c03\u7528API\u7684\u5bc6\u94a5 \u5fae\u4fe1\u652f\u4ed8\u65f6\u9700\u8981\u586b\u5165"}
|
||||
|
||||
# weixin4j\u7684\u4e34\u65f6\u76ee\u5f55
|
||||
# \u53ef\u80fd\u5b58\u653etoken\u6587\u4ef6\u3001\u4e8c\u7ef4\u7801\u6587\u4ef6\u3001\u5a92\u4f53\u6587\u4ef6\u3001\u5bf9\u8d26\u5355\u6587\u4ef6\u7b49
|
||||
# \u4e3a\u7a7a\u65f6\u5219\u83b7\u53d6java.io.tmpdir\u4e34\u65f6\u76ee\u5f55
|
||||
weixin4j.tmpdir=
|
||||
# \u5fae\u4fe1\u652f\u4ed8\u67d0\u4e9b\u63a5\u53e3\u9700\u8981\u7684ca\u8bc1\u4e66\u5b58\u653e\u7684\u5b8c\u6574\u8def\u5f84
|
||||
# classpath\u8def\u5f84\u4e0b\u53ef\u4ee5\u8fd9\u4e48\u5199
|
||||
# weixin4j.certificate.file=classpath:xxxxx.p12
|
||||
# \u4e3a\u7a7a\u65f6\u5219\u83b7\u53d6classpath\u6839\u76ee\u5f55\u4e0b\u7684ca.p12\u6587\u4ef6
|
||||
weixin4j.certificate.file=/tmp/weixin4j/xxxxx.p12
|
||||
|
||||
# \u7528\u6237oauth\u6388\u6743\u540e\u91cd\u5b9a\u5411\u7684url(\u5728\u4f7f\u7528OauthApi\u65f6\u586b\u5199)
|
||||
weixin4j.user.oauth.redirect.uri=
|
||||
Reference in New Issue
Block a user