【微信公众号】java实现微信公众号的你问我答

从文档上面看,微信公众号的通信是通过xml形式来传递信息的,由此我们需要频繁地将对象转成xml,以及将xml转换成map集合

定义TextMessage用于存储文本相关信息

/**
 * @author evan_qb
 * @date 2018/8/14 9:03
 */
@Data
public class TextMessage{
    private String ToUserName;

    private String FromUserName;

    private Long CreateTime;

    private String MsgType;
    private String Content;
    private String MsgId;
}

由于频繁使用xml和对象的转换,我们将其抽取工具类

首先引入相关的依赖dom4j以及xStream

<!--xml转换相关-->
<dependency>
     <groupId>dom4j</groupId>
     <artifactId>dom4j</artifactId>
     <version>1.6.1</version>
</dependency>
<dependency>
     <groupId>com.thoughtworks.xstream</groupId>
     <artifactId>xstream</artifactId>
     <version>1.4.4</version>
</dependency>

工具类MessageUtil

/**
 * @author evan_qb
 * @date 2018/8/14 8:57
 */
public class MessageUtil {

    /**
     * xml转Map
     * @param request
     * @return
     * @throws IOException
     * @throws DocumentException
     */
    public static Map<String,String> xmlToMap(HttpServletRequest request) {
        Map<String,String> map = new HashMap<>();
        SAXReader reader = new SAXReader();
        ServletInputStream in = null;
        try{
            in = request.getInputStream();
            Document doc = reader.read(in);
            Element root = doc.getRootElement();
            List<Element> list = root.elements();
            for (Element e: list){
                map.put(e.getName(),e.getText());
            }
            in.close();
        }catch (Exception e){

        }
        return map;
    }

    /**
     * 文本对象转换成xml
     * @param message
     * @return
     */
    public static  String textMessageToXml(Object message){
        XStream xStream = new XStream();
        xStream.alias("xml",message.getClass());
        return xStream.toXML(message);
    }

    /**
     * 初始化文本信息
     * @param toUserName
     * @param fromUserName
     * @param msgType
     * @param content
     * @return
     */
    public static String initText(String toUserName,String fromUserName,String msgType,String content){
        TextMessage textMessage = new TextMessage();
        textMessage.setFromUserName(toUserName);
        textMessage.setToUserName(fromUserName);
        textMessage.setMsgType(msgType);
        textMessage.setCreateTime(new Date().getTime());
        textMessage.setContent(content);
        return MessageUtil.textMessageToXml(textMessage);
    }

}

接下来编写Controller进行测试:

/**
 * 实现你问我答
 * @param request
 * @return
 */
@PostMapping("/verify")
public String replyMessage(HttpServletRequest request){
    Map<String,String> map = MessageUtil.xmlToMap(request);
    String toUserName = map.get("ToUserName");
    String fromUserName = map.get("FromUserName");
    String msgType = map.get("MsgType");
    String content = map.get("Content");
    String message = null;
    if (MessageType.MESSAGE_TEXT.equals(msgType)){
        //回复文本信息触发的消息
        message = MessageUtil.initText(toUserName,fromUserName,MessageType.MESSAGE_TEXT,content);

    }else if (MessageType.MESSAGE_EVENT.equals(msgType)){
        //关注时触发的消息
        String event = map.get("Event");
        if (MessageType.MESSAGE_SUBSCRIBE.equals(event)){
            message = MessageUtil.initText(toUserName,fromUserName,MessageType.MESSAGE_TEXT,"欢迎关注evan波的公众号");
        }
    }
    return message;
}

测试结果:

 

 

 

展开阅读全文

没有更多推荐了,返回首页