This commit is contained in:
jinyu
2016-10-17 16:32:28 +08:00
parent 2325132ac7
commit 21b1f9ecad
132 changed files with 1057 additions and 193 deletions
@@ -0,0 +1,19 @@
package com.foxinmy.weixin4j.example.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.foxinmy.weixin4j.mp.WeixinProxy;
@Controller
@RequestMapping("")
public class IndexController {
@Autowired
private WeixinProxy weixinProxy;
@RequestMapping
public String index() {
return "index";
}
}
@@ -1,20 +1,18 @@
package com.foxinmy.weixin4j.example.server;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.DebugMessageHandler;
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
/**
* 微信消息服务:单独作为一个服务jar包启动
* 微信消息服务:单独作为一个服务jar包启动推荐这种方式去处理微信消息可在本项目的根目录运行 `mvn package`得到一个可执行的zip包
*
* @className Weixin4jServerStartupWithoutThread
* @className Weixin4jServerStartup
* @author jinyu(foxinmy@gmail.com)
* @date 2015年5月7日
* @since JDK 1.7
* @see
* @since JDK 1.6
*/
public class Weixin4jServerStartupWithoutThread {
public final class Weixin4jServerStartup {
/**
* 服务监听的端口号,目前微信只支持80端口,可以考虑用nginx做转发到此端口
*/
@@ -39,15 +37,11 @@ public class Weixin4jServerStartupWithoutThread {
* @param args
* @throws WeixinException
*/
@SuppressWarnings("resource")
public static void main(String[] args) throws WeixinException {
// 单独服务启动
// new WeixinServerBootstrap(aesToken)
// .handlerPackagesToScan(handlerPackage)
// .addHandler(DebugMessageHandler.global).openAlwaysResponse()
// .startup(port);
// spring容器启动
ApplicationContext app = new ClassPathXmlApplicationContext(
new String[] { "classpath:/spring-bean.xml" });
new WeixinServerBootstrap(aesToken) // 指定开发者token信息
.handlerPackagesToScan(handlerPackage) // 扫描处理消息的包
.addHandler(DebugMessageHandler.global) // 当没有匹配到消息处理时输出调试信息开发环境打开
.openAlwaysResponse() // 当没有匹配到消息处理时输出空白回复(公众号不会出现该公众号无法提供服务的提示)正式环境打开
.startup(port); // 绑定服务的端口号即对外暴露(微信服务器URL地址)的服务端口
}
}
}
@@ -8,6 +8,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.handler.DebugMessageHandler;
import com.foxinmy.weixin4j.spring.SpringBeanFactory;
import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
import com.foxinmy.weixin4j.util.AesToken;
@@ -15,13 +16,14 @@ import com.foxinmy.weixin4j.util.AesToken;
import io.netty.util.internal.logging.InternalLoggerFactory;
/**
* 微信消息服务:需要另外开启一个线程去启动服务,比如在spring mvc中
* 微信消息服务:需要另外开启一个线程去启动服务,这里值得注意的时:weixin4j-serve本身是作为一个单独的服务来启动的,可以不依赖Spring容器,
* 但考虑到目前都是Spring mvc的架构,这里就需要使用一个独立的线程去启动服务,其实本身没有使用spring mvc的API
* 以后会考虑支持servlet api去集成不同的web框架。
*
* @className Weixin4jServerStartupWithThread
* @author jinyu(foxinmy@gmail.com)
* @date 2015年5月7日
* @since JDK 1.7
* @see
* @since JDK 1.6
*/
public class Weixin4jServerStartupWithThread implements ApplicationContextAware {
/**
@@ -71,11 +73,13 @@ public class Weixin4jServerStartupWithThread implements ApplicationContextAware
@Override
public void run() {
try {
new WeixinServerBootstrap(aesToken)
.handlerPackagesToScan(handlerPackage)
new WeixinServerBootstrap(aesToken) // 指定开发者token信息。
.handlerPackagesToScan(handlerPackage) // 扫描处理消息的包。
.resolveBeanFactory(
new SpringBeanFactory(applicationContext))
.openAlwaysResponse().startup(port);
new SpringBeanFactory(applicationContext)) // 声明处理消息类由Spring容器去实例化。
.addHandler(DebugMessageHandler.global) // 当没有匹配到消息处理时输出调试信息,开发环境打开。
.openAlwaysResponse() // 当没有匹配到消息处理时输出空白回复(公众号不会出现「该公众号无法提供服务的提示」),正式环境打开。
.startup(port); // 绑定服务的端口号,即对外暴露(微信服务器URL地址)的服务端口。
} catch (WeixinException e) {
InternalLoggerFactory.getInstance(getClass()).error(
"weixin4j server startup:FAIL", e);
@@ -14,8 +14,7 @@ import com.foxinmy.weixin4j.response.WeixinResponse;
* @className HelloMessageHandler
* @author jinyu(foxinmy@gmail.com)
* @date 2015年12月27日
* @since JDK 1.7
* @see
* @since JDK 1.6
*/
@Component
public class HelloMessageHandler extends TextMessageHandler {
@@ -15,15 +15,14 @@ import com.foxinmy.weixin4j.response.WeixinResponse;
* @className SubscribeMessageHandler
* @author jinyu(foxinmy@gmail.com)
* @date 2015年12月3日
* @since JDK 1.7
* @see
* @since JDK 1.6
*/
@Component
public class SubscribeMessageHandler extends
MessageHandlerAdapter<ScribeEventMessage> {
@Override
public WeixinResponse doHandle0(WeixinRequest arg0, ScribeEventMessage arg1)
public WeixinResponse doHandle0(WeixinRequest request, ScribeEventMessage message)
throws WeixinException {
return new TextResponse("欢迎关注~");
}
@@ -15,8 +15,7 @@ import com.foxinmy.weixin4j.response.WeixinResponse;
* @className TextMessageHandler
* @author jinyu(foxinmy@gmail.com)
* @date 2015年11月18日
* @since JDK 1.7
* @see
* @since JDK 1.6
*/
@Component
public class TextMessageHandler extends MessageHandlerAdapter<TextMessage> {
@@ -15,8 +15,7 @@ import com.foxinmy.weixin4j.response.WeixinResponse;
* @className VoiceMessageHandler
* @author jinyu(foxinmy@gmail.com)
* @date 2015年11月18日
* @since JDK 1.7
* @see
* @since JDK 1.6
*/
@Component
public class VoiceMessageHandler extends MessageHandlerAdapter<VoiceMessage> {