微信推送给服务器的XML消息解析;
可以使用request.getInputStream(); 获取输入的消息流;但是需要自己解析流;
spring mvc自带解析功能;
controller中:
@RequestMapping(value="weixin_create" ) @ResponseBody public String weixinCreate(@RequestBody Scan scan ){ scanMapper.insertSelective(scan ); return "weixinCreate..."; }
而且,还可以获取get串中的参数;
@RequestMapping(value="weixin_create" ) @ResponseBody public String weixinCreate(@RequestBody Scan scan, String signature){ scan.setToUserName(signature); // 测试是否可以获得值 scanMapper.insertSelective(scan); return "weixinCreate..."; }
model对象,这个也是mybatis的使用对象;
package com.xxx.model; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="xml") @XmlAccessorType(XmlAccessType.FIELD) public class Scan { private Integer id; @XmlElement(name="ToUserName") private String toUserName; @XmlElement(name="FromUserName") private String fromUserName; @XmlElement(name="CreateTime") private Integer createTime; @XmlElement(name="MsgType") private String msgType; @XmlElement(name="Event") private String event; @XmlElement(name="EventKey") private String eventKey; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getToUserName() { return toUserName; } public void setToUserName(String toUserName) { this.toUserName = toUserName; } public String getFromUserName() { return fromUserName; } public void setFromUserName(String fromUserName) { this.fromUserName = fromUserName; } public Integer getCreateTime() { return createTime; } public void setCreateTime(Integer createTime) { this.createTime = createTime; } public String getMsgType() { return msgType; } public void setMsgType(String msgType) { this.msgType = msgType; } public String getEvent() { return event; } public void setEvent(String event) { this.event = event; } public String getEventKey() { return eventKey; } public void setEventKey(String eventKey) { this.eventKey = eventKey; } }