1 package com.foxinmy.weixin4j.server.test;
2
3 import io.netty.channel.ChannelHandlerContext;
4
5 import org.springframework.context.ApplicationContext;
6
7 import com.foxinmy.weixin4j.dispatcher.BeanFactory;
8 import com.foxinmy.weixin4j.handler.DebugMessageHandler;
9 import com.foxinmy.weixin4j.handler.MessageHandlerAdapter;
10 import com.foxinmy.weixin4j.handler.MultipleMessageHandlerAdapter;
11 import com.foxinmy.weixin4j.handler.WeixinMessageHandler;
12 import com.foxinmy.weixin4j.interceptor.WeixinMessageInterceptor;
13 import com.foxinmy.weixin4j.message.TextMessage;
14 import com.foxinmy.weixin4j.message.VoiceMessage;
15 import com.foxinmy.weixin4j.mp.event.ScanEventMessage;
16 import com.foxinmy.weixin4j.request.WeixinMessage;
17 import com.foxinmy.weixin4j.request.WeixinRequest;
18 import com.foxinmy.weixin4j.response.TextResponse;
19 import com.foxinmy.weixin4j.response.WeixinResponse;
20 import com.foxinmy.weixin4j.spring.SpringBeanFactory;
21 import com.foxinmy.weixin4j.startup.WeixinServerBootstrap;
22
23
24
25
26
27
28
29
30
31
32 public class MessageServerStartup {
33
34
35 final String weixinId = "wx4ab8f8de58159a57";
36
37 final String token = "weixin4j";
38
39 final String aesKey = "";
40
41
42
43
44
45 public void test1() {
46
47 new WeixinServerBootstrap(token).addHandler(DebugMessageHandler.global).startup();
48
49
50
51 }
52
53
54
55
56
57 public void test2() {
58
59 WeixinMessageHandler textMessageHandler = new MessageHandlerAdapter<TextMessage>() {
60 @Override
61 protected WeixinResponse doHandle0(TextMessage message) {
62 return new TextResponse("HelloWorld!");
63 }
64 };
65
66 WeixinMessageHandler voiceMessageHandler = new MessageHandlerAdapter<VoiceMessage>() {
67 @Override
68 protected WeixinResponse doHandle0(VoiceMessage message) {
69 return new TextResponse("HelloWorld!");
70 }
71 };
72
73 new WeixinServerBootstrap(weixinId, token, aesKey)
74 .addHandler(textMessageHandler, voiceMessageHandler, DebugMessageHandler.global).startup();
75 }
76
77
78
79
80
81 public void test3() {
82 @SuppressWarnings("unchecked")
83 MultipleMessageHandlerAdapter messageHandler = new MultipleMessageHandlerAdapter(ScanEventMessage.class,
84 TextMessage.class) {
85 @Override
86 public WeixinResponse doHandle(WeixinRequest request,
87 WeixinMessage message) {
88 return new TextResponse("处理了扫描和文字消息");
89 }
90 };
91 new WeixinServerBootstrap(token).addHandler(messageHandler, DebugMessageHandler.global).startup();
92 }
93
94
95
96
97
98
99 public void test4() {
100
101 String packageToScan = "com.foxinmy.weixin4j.handler";
102
103
104 ApplicationContext applicationContext = null;
105 BeanFactory beanFactory = new SpringBeanFactory(applicationContext);
106 new WeixinServerBootstrap(token).handlerPackagesToScan(packageToScan).openAlwaysResponse()
107 .resolveBeanFactory(beanFactory).startup();
108 }
109
110
111
112
113
114
115 public void test5() {
116
117 WeixinMessageInterceptor interceptor = new WeixinMessageInterceptor() {
118 @Override
119 public boolean preHandle(ChannelHandlerContext context, WeixinRequest request, WeixinMessage message,
120 WeixinMessageHandler handler) {
121 context.writeAndFlush(new TextResponse("所有消息被拦截了!"));
122 return false;
123 }
124
125 @Override
126 public void postHandle(ChannelHandlerContext context, WeixinRequest request, WeixinResponse response,
127 WeixinMessage message, WeixinMessageHandler handler) {
128 System.err.println("preHandle返回为true,执行handler后");
129 }
130
131 @Override
132 public void afterCompletion(ChannelHandlerContext context, WeixinRequest request, WeixinResponse response,
133 WeixinMessage message, WeixinMessageHandler handler, Exception exception) {
134 System.err.println("请求处理完毕");
135 }
136
137 @Override
138 public int weight() {
139 return 0;
140 }
141 };
142 new WeixinServerBootstrap(token).addInterceptor(interceptor).openAlwaysResponse().startup();
143 }
144
145
146
147
148
149
150
151 public static void main(String[] args) throws Exception {
152 new MessageServerStartup().test1();
153 }
154 }