diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/WeixinMessageDispatcher.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/WeixinMessageDispatcher.java index 2eff2fc9..6423ee44 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/WeixinMessageDispatcher.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/dispatcher/WeixinMessageDispatcher.java @@ -30,7 +30,7 @@ import com.foxinmy.weixin4j.handler.WeixinMessageHandler; import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor; import com.foxinmy.weixin4j.request.WeixinMessage; import com.foxinmy.weixin4j.request.WeixinRequest; -import com.foxinmy.weixin4j.response.TextResponse; +import com.foxinmy.weixin4j.response.BlankResponse; import com.foxinmy.weixin4j.response.WeixinResponse; import com.foxinmy.weixin4j.type.AccountType; import com.foxinmy.weixin4j.util.ClassUtil; @@ -90,11 +90,10 @@ public class WeixinMessageDispatcher { * 消息转换 */ private Map, Unmarshaller> messageUnmarshaller; - /** - * 开启debug:未匹配到MessageHanlder输出消息信息 + * 是否总是响应请求,如未匹配到MessageHandler时回复空白消息 */ - private boolean isDebug; + private boolean alwaysResponse; public WeixinMessageDispatcher() { this(new DefaultMessageMatcher()); @@ -119,8 +118,9 @@ public class WeixinMessageDispatcher { public void doDispatch(final ChannelHandlerContext context, final WeixinRequest request, final CruxMessageHandler cruxMessage) throws WeixinException { - WeixinMessageKey messageKey = defineMessageKey(cruxMessage.getMsgType(), - cruxMessage.getEventType(), cruxMessage.getAccountType()); + WeixinMessageKey messageKey = defineMessageKey( + cruxMessage.getMsgType(), cruxMessage.getEventType(), + cruxMessage.getAccountType()); Class targetClass = messageMatcher .match(messageKey); Object message = messageRead(request.getOriginalContent(), targetClass); @@ -159,8 +159,8 @@ public class WeixinMessageDispatcher { * 账号类型 * @return */ - protected WeixinMessageKey defineMessageKey(String messageType, String eventType, - AccountType accountType) { + protected WeixinMessageKey defineMessageKey(String messageType, + String eventType, AccountType accountType) { return new WeixinMessageKey(messageType, eventType, accountType); } @@ -176,17 +176,8 @@ public class WeixinMessageDispatcher { */ protected void noHandlerFound(ChannelHandlerContext context, WeixinRequest request, Object message) { - if (isDebug) { - if (message == null) { - context.writeAndFlush( - new TextResponse(request.getOriginalContent() - .replaceAll("\\!\\[CDATA\\[", "") - .replaceAll("\\]\\]", ""))).addListener( - ChannelFutureListener.CLOSE); - } else { - context.writeAndFlush(new TextResponse(message.toString())) - .addListener(ChannelFutureListener.CLOSE); - } + if (alwaysResponse) { + context.write(BlankResponse.global); } else { context.writeAndFlush( HttpUtil.createHttpResponse(null, NOT_FOUND, null)) @@ -456,7 +447,10 @@ public class WeixinMessageDispatcher { return this.messageMatcher; } - public void openDebugMode() { - isDebug = true; + /** + * 打开总是响应开关,如未匹配到MessageHandler时回复空白消息 + */ + public void openAlwaysResponse() { + this.alwaysResponse = true; } } diff --git a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/startup/WeixinServerBootstrap.java b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/startup/WeixinServerBootstrap.java index 7552c17f..d16b2655 100644 --- a/weixin4j-server/src/main/java/com/foxinmy/weixin4j/startup/WeixinServerBootstrap.java +++ b/weixin4j-server/src/main/java/com/foxinmy/weixin4j/startup/WeixinServerBootstrap.java @@ -18,8 +18,8 @@ import java.util.Map; import com.foxinmy.weixin4j.dispatcher.BeanFactory; import com.foxinmy.weixin4j.dispatcher.DefaultMessageMatcher; -import com.foxinmy.weixin4j.dispatcher.WeixinMessageKey; import com.foxinmy.weixin4j.dispatcher.WeixinMessageDispatcher; +import com.foxinmy.weixin4j.dispatcher.WeixinMessageKey; import com.foxinmy.weixin4j.dispatcher.WeixinMessageMatcher; import com.foxinmy.weixin4j.exception.WeixinException; import com.foxinmy.weixin4j.handler.WeixinMessageHandler; @@ -310,17 +310,18 @@ public final class WeixinServerBootstrap { * 消息类 * @return */ - public WeixinServerBootstrap registMessageClass(WeixinMessageKey messageKey, + public WeixinServerBootstrap registMessageClass( + WeixinMessageKey messageKey, Class messageClass) { messageDispatcher.registMessageClass(messageKey, messageClass); return this; } /** - * 开启debug:未匹配到MessageHanlder输出消息信息 + * 打开总是响应开关,如未匹配到MessageHandler时回复空白消息 */ - public WeixinServerBootstrap openDebugMode() { - messageDispatcher.openDebugMode(); + public WeixinServerBootstrap openAlwaysResponse() { + messageDispatcher.openAlwaysResponse(); return this; } } \ No newline at end of file