SpringBoot对接公众号,自动消息回复

回复的消息

服务端开发 1)pom文件引入dom4j jar包,用来解析xml

<dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
        </dependency>

2)新建bean类用来解析xml使用

/**
 * 
 * 类名称: BaseMessage
 * 类描述: 回复消息的基类
 * @author yuanjun
 * 创建时间:2017年12月8日上午11:38:11
 */
public class BaseMessage {
          
   

    protected String ToUserName;
    protected String FromUserName;
    protected long CreateTime;
    protected String MsgType;

    public BaseMessage() {
        super();
    }

    public String getToUserName() {
        return ToUserName;
    }

    public void setToUserName(String toUserName) {
        ToUserName = toUserName;
    }

    public String getFromUserName() {
        return FromUserName;
    }

    public void setFromUserName(String fromUserName) {
        FromUserName = fromUserName;
    }

    public long getCreateTime() {
        return CreateTime;
    }

    public void setCreateTime(long createTime) {
        CreateTime = createTime;
    }

    public String getMsgType() {
        return MsgType;
    }

    public void setMsgType(String msgType) {
        MsgType = msgType;
    }

}
/**
 * 
 * 类名称: MessageTest 类描述: 消息内容实体
 * 
 * @author yuanjun 创建时间:2017年12月5日上午10:41:40
 */
public class MessageText extends BaseMessage {
          
   

    private String Content;// 文本消息内容

    private String MsgId;// 消息id,64位整型

    public MessageText() {

    }

    public MessageText(String toUserName, String fromUserName, long createTime, String msgType, String content,
            String msgId) {
        super();
        ToUserName = toUserName;
        FromUserName = fromUserName;
        CreateTime = createTime;
        MsgType = msgType;
        Content = content;
        MsgId = msgId;
    }

    public String getContent() {
        return Content;
    }

    public void setContent(String content) {
        Content = content;
    }

    public String getMsgId() {
        return MsgId;
    }

    public void setMsgId(String msgId) {
        MsgId = msgId;

    }
}

xml解析工具类

public class MessageUtil {
    public static Map<String,String> xmlToMap(HttpServletRequest request)
    {
        Map<String,String> map = new HashMap<>();
        SAXReader reader = new SAXReader();

        InputStream in = null;
        try {
            in = request.getInputStream();
            Document doc = reader.read(in);
            Element root = doc.getRootElement();
            List<Element> list = root.elements();
            for (Element element : list) {
                map.put(element.getName(), element.getText());
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            try {
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return map;


    }

}

message转xml工具类

controller代码

经验分享 程序员 微信小程序 职场和发展