-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
MP_同步回复消息
Binary Wang edited this page Nov 9, 2023
·
4 revisions
WxMessageRouter
默认使用异步的方式处理消息,如果要使用同步回复微信消息,那么:
- 需要将路由规则配置为同步
- 且handler需要返回一个
WxMpXmlOutMessage
// 将WxXmlMessage交给消息路由器
HttpServletRequest request = ...;
WxXmlMessage inMsg = ...;
WxMpXmlOutMessage reMsg = router.route(inMsg);
if (reMsg != null) {
// 说明是同步回复的消息
// 将xml写入HttpServletResponse
response.setCharacterEncoding("UTF-8");
response.getWriter().write(reMsg.toXml());
} else {
// 说明是异步回复的消息,直接将空字符串写入HttpServletResponse
}
WxMpXmlOutTextMessage
是同步回复给微信消息的对象,不同类型的消息类型可以用不同的方式构造:
WxMpXmlOutMessage.TEXT()
.content("content")
.fromUser("to")
.toUser("from")
.build();
WxMpXmlOutMessage.IMAGE()
.mediaId("ddfefesfsdfef")
.fromUser("to")
.toUser("from")
.build();
WxMpXmlOutMessage.VOICE()
.mediaId("ddfefesfsdfef")
.fromUser("to")
.toUser("from")
.build();
WxMpXmlOutMessage.VIDEO()
.mediaId("media_id")
.fromUser("toUser")
.toUser("fromUser")
.title("title")
.description("ddfff")
.build();
WxMpXmlOutMessage.MUSIC()
.fromUser("toUser")
.toUser("fromUser")
.title("title")
.description("ddfff")
.hqMusicUrl("hQMusicUrl")
.musicUrl("musicUrl")
.thumbMediaId("thumbMediaId")
.build();
WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
item.setDescription("description");
item.setPicUrl("picUrl");
item.setTitle("title");
item.setUrl("url");
WxMpXmlOutNewsMessage m = WxMpXmlOutMessage.NEWS()
.fromUser("toUser")
.toUser("fromUser")
.addArticle(item)
.build();