response.getWriter().write(Content(Content,FromUserName,ToUserName));// 将回应发送给微信服务器
public static String Content(String Content,String FromUserName,String ToUserName)
{
// 接收用户发送的文本消息内容
//创建图文消息
NewsMessage newsMessage = new NewsMessage();
newsMessage.setToUserName(ToUserName);
newsMessage.setFromUserName(FromUserName);
newsMessage.setCreateTime(new Date().getTime());
newsMessage.setMsgType(MessageUtil.RESP_MESSAGE_TYPE_NEWS);
newsMessage.setFuncFlag(0);
List<Article> articleList = new ArrayList<Article>();
//单图文消息
Article article = new Article();
article.setTitle("新闻标题");
article.setDescription("新闻简介");
article.setPicUrl("http://www.119ypt.com/cure/upload/1111111.jpg");
article.setUrl("http://blog.csdn.net/lyq8479");
articleList.add(article);
//设置图文消息个数
newsMessage.setArticleCount(articleList.size());
//设置图文消息包含的图文集合
newsMessage.setArticles(articleList);
//将图文消息对象转换成xml字符串
MessageUtil.messageToXml(newsMessage);
return MessageUtil.messageToXml(newsMessage);
}
/**
* 图文消息对象转换成xml
* @param newsMessage
* 图文消息对象
* @return xml
*/
public static String messageToXml(NewsMessage newsMessage) {
xstream.alias("xml", newsMessage.getClass());
xstream.alias("item", new Article().getClass());
return xstream.toXML(newsMessage);
}
<xml>
<ToUserName><![CDATA[gh_a2f90ff244eb]]></ToUserName>
<FromUserName><![CDATA[oqDVT0t9c32X81SlQwjG4YoznAac]]></FromUserName>
<CreateTime><![CDATA[1577348731280]]></CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<FuncFlag><![CDATA[0]]></FuncFlag>
<ArticleCount><![CDATA[1]]></ArticleCount>
<Articles>
<item>
<Title><![CDATA[新闻标题]]></Title>
<Description><![CDATA[新闻简介]]></Description>
<PicUrl><![CDATA[http://www.119ypt.com/cure/upload/1111111.jpg]]></PicUrl>
<Url><![CDATA[http://blog.csdn.net/lyq8479]]></Url>
</item>
</Articles>
</xml>