View Javadoc
1   package com.foxinmy.weixin4j.example.server.handler;
2   
3   import org.springframework.stereotype.Component;
4   
5   import com.foxinmy.weixin4j.message.TextMessage;
6   import com.foxinmy.weixin4j.request.WeixinRequest;
7   import com.foxinmy.weixin4j.response.TextResponse;
8   import com.foxinmy.weixin4j.response.WeixinResponse;
9   
10  /**
11   * 输入 hello 回复 world
12   *
13   * @className HelloMessageHandler
14   * @author jinyu(foxinmy@gmail.com)
15   * @date 2015年12月27日
16   * @since JDK 1.6
17   */
18  @Component
19  public class HelloMessageHandler extends TextMessageHandler {
20  
21      @Override
22      public boolean canHandle0(WeixinRequest request, TextMessage message) {
23          /**
24           * 用户输入hello时
25           */
26          return message.getContent().equalsIgnoreCase("hello");
27      }
28  
29      @Override
30      public WeixinResponse doHandle0(TextMessage message) {
31          /**
32           * 返回用户「world」文本
33           */
34          return new TextResponse("world");
35      }
36  
37      /**
38       * 因为HelloMessageHandler和TextMessageHandler都会匹配到文本消息
39       * 所以这里需要提高下权重(大于TextMessageHandler就行了) > TextMessageHandler
40       */
41      @Override
42      public int weight() {
43          return super.weight() + 1;
44      }
45  }