目录
接收到的数据信息如下,小视频唯一区别就是MsgType=shortvideo
当任何一个微信用户,打开你的公众号,通过键盘输入文字、选择图片等信息发送时,你设置的接口,就会收到对方的信息。这个信息结构大致和被动回复(前面讲的点击菜单回复)类似。
文本消息
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[this is a test]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>
接收对象WXMessgeBean。
import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@XmlRootElement(name="xml")
@XmlAccessorType(XmlAccessType.FIELD)
public class WXMessgeBean {
private String FromUserName;
private String ToUserName;
private long CreateTime;
private String MsgType;
private String Event;
private String EventKey;
private String Content;
private String Url;
private String PicUrl;
private String MediaId;
private String Format;
private String Recognition;
private String ThumbMediaId;
//地理位置
private float Location_X;
private float Location_Y;
private float Scale;
private long MsgId;
//省略了stter和getter
......
}
接收代码,当接收到对方的消息后,我们给他回复一句问候。
@PostMapping(value = "/checktoken", produces = MediaType.APPLICATION_XML_VALUE)
public String receiveWX(HttpServletRequest request, HttpServletResponse response ){
try {
Marshaller marshaller;
Unmarshaller unmarshal;
//你要解析成哪个bean对象,newInstance的参数就是哪个对象
JAXBContext jaxbContext = JAXBContext.newInstance(WXMessgeBean.class);
unmarshal = jaxbContext.createUnmarshaller();
//xml解码成bean对象
WXMessgeBean wxMessgeBean = (WXMessgeBean) unmarshal.unmarshal(request.getInputStream());
if ("text".equals(wxMessgeBean.getMsgType())){
WXMessgeBean bean = new WXMessgeBean();
bean.setFromUserName(wxMessgeBean.getToUserName());
bean.setToUserName(wxMessgeBean.getFromUserName());
bean.setCreateTime(new Date().getTime());
bean.setMsgType("text");
bean.setContent("你好,欢迎浏览我的公众号");
marshaller = jaxbContext.createMarshaller();
StringWriter writer = new StringWriter();
marshaller.marshal(bean,writer);
return writer.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
接收到的信息如下:
图像消息
当用户从相册选择一个图片后,系统会将此图片提交到服务端,并生成PicUrl和MediaId
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
<PicUrl><![CDATA[this is a url]]></PicUrl>
<MediaId><![CDATA[media_id]]></MediaId>
<MsgId>1234567890123456</MsgId>
</xml>
语音消息
<xml>
<ToUserName>< ![CDATA[toUser] ]></ToUserName>
<FromUserName>< ![CDATA[fromUser] ]></FromUserName>
<CreateTime>1357290913</CreateTime>
<MsgType>< ![CDATA[voice] ]></MsgType>
<MediaId>< ![CDATA[media_id] ]></MediaId>
<Format>< ![CDATA[Format] ]></Format>
<Recognition>< ![CDATA[腾讯微信团队] ]></Recognition>
<MsgId>1234567890123456</MsgId>
</xml>
接收方法同上,接收信息如下:
视频及小视频消息
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1357290913</CreateTime>
<MsgType><![CDATA[video]]></MsgType>
<MediaId><![CDATA[media_id]]></MediaId>
<ThumbMediaId><![CDATA[thumb_media_id]]></ThumbMediaId>
<MsgId>1234567890123456</MsgId>
</xml>
接收到的数据信息如下,小视频唯一区别就是MsgType=shortvideo
地理位置
<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1351776360</CreateTime>
<MsgType><![CDATA[location]]></MsgType>
<Location_X>23.134521</Location_X>
<Location_Y>113.358803</Location_Y>
<Scale>20</Scale>
<Label><![CDATA[位置信息]]></Label>
<MsgId>1234567890123456</MsgId>
</xml>