springboot的微信公众号(九)图文消息

113 阅读3分钟

图文消息也叫作新闻消息,我们从这里可以看个大概的结构,标题、图片、简介、然后你点进去是个链接

我们例行看一下官方文档,这个解释的比较清楚

xml里面是个Articles,而且是个数组,也就是说可以发多个,但是有上限的

通过前面的文章,我们应该想着怎么去建这个实体类:一个msg,属性包含数组,数组指向Article,大概就是这样,你可以试着先建一个

这里先贴出来实体类,相关的解释注释都有


import java.util.Date;
import java.util.List;

public class NewsMsg extends BaseMessage{
	private int ArticleCount;//文章的个数
    private List<News> Articles;
	public NewsMsg(){
		this.setMsgType("news");
		this.setCreateTime(new Date().getTime());
	}
	public int getArticleCount() {
		return ArticleCount;
	}
	public void setArticleCount(int articleCount) {
		ArticleCount = articleCount;
	}
	public List<News> getArticles() {
		return Articles;
	}
	public void setArticles(List<News> articles) {
		Articles = articles;
	}
	
	

}
public class News {
	private String Title;//图文消息标题
	private String Description;//图文消息描述
	private String PicUrl;//图片链接,支持JPG、PNG格式,较好的效果为大图360*200,小图200*200
	private String Url;//点击图文消息跳转链接
	public String getTitle() {
		return Title;
	}
	public void setTitle(String title) {
		Title = title;
	}
	public String getDescription() {
		return Description;
	}
	public void setDescription(String description) {
		Description = description;
	}
	public String getPicUrl() {
		return PicUrl;
	}
	public void setPicUrl(String picUrl) {
		PicUrl = picUrl;
	}
	public String getUrl() {
		return Url;
	}
	public void setUrl(String url) {
		Url = url;
	}
}

然后调用

if ("3".equals(content)) {
			MsgUtil.sendNewsMsgToUser( openId,formUser,response);
		}

然后执行发送

/**
	 * 发送图文消息
	 * @param openId
	 * @param formUser
	 * @param response
	 */
	public static void sendNewsMsgToUser(String openId, String formUser, HttpServletResponse response) {
		NewsMsg msg = new NewsMsg();
		List<News> articles = new ArrayList<News>();
		News news = new News();
		news.setTitle("猫和老鼠");
		news.setDescription("《猫和老鼠》以闹剧为特色,描绘了一对水火不容的冤家:汤姆和杰瑞猫鼠之间的战争,片中的汤姆经常使用狡诈的诡计来对付杰瑞,而杰瑞则时常利用汤姆诡计中的漏洞逃脱他的迫害并给予报复");
		news.setPicUrl(WeiXinUtil.NATURL+"/wx-demo/jerry.jpg");
		news.setUrl("https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=jerry&fr=ala&ala=1&alatpl=adress&pos=0&hs=2&xthttps=111111");
		articles.add(news);
		msg.setArticleCount(articles.size());
		msg.setArticles(articles );
		msg.setFromUserName(formUser);
		msg.setToUserName(openId);
		String textMessageToXml = XMLUtils.newsMessageToXml(msg);
		logger.info("发送的消息是-------"+textMessageToXml);
		PrintWriter out=null;
		try {
			out = response.getWriter();
			//写入返回的response中
			out.println(textMessageToXml);
		} catch (IOException e) {
			logger.error("发送微信消息失败",e);
			e.printStackTrace();
		}finally{
			//注意关流,不然会发送失败
			out.close();
		}
	}

注意这里有一个地方跟其他的不一样,就是xml转为字符串的方法,因为它是类中包含类的数组的,所以会多一个

然后成功的样例就是上面的那个

封装的xml数组如下:

2018-11-20 10:34:52.718  INFO 5196 --- [nio-8080-exec-8] com.example.weixin.demo.utils.MsgUtil    : 发送的消息是-------<xml>
  <ToUserName>oHz9D0TObxbKYC720R61svLa0YDg</ToUserName>
  <FromUserName>gh_7781317207c9</FromUserName>
  <CreateTime>1542681292687</CreateTime>
  <MsgType>news</MsgType>
  <ArticleCount>1</ArticleCount>
  <Articles>
    <item>
      <Title>猫和老鼠</Title>
      <Description>《猫和老鼠》以闹剧为特色,描绘了一对水火不容的冤家:汤姆和杰瑞猫鼠之间的战争,片中的汤姆经常使用狡诈的诡计来对付杰瑞,而杰瑞则时常利用汤姆诡计中的漏洞逃脱他的迫害并给予报复</Description>
      <PicUrl>http://pnvcvv.natappfree.cc/wx-demo/jerry.jpg</PicUrl>
      <Url>https://image.baidu.com/search/index?tn=baiduimage&amp;ct=201326592&amp;lm=-1&amp;cl=2&amp;ie=gb18030&amp;word=jerry&amp;fr=ala&amp;ala=1&amp;alatpl=adress&amp;pos=0&amp;hs=2&amp;xthttps=111111</Url>
    </item>
  </Articles>
</xml>

被动回复消息当然还有其他的消息,语音消息、视频消息等等,有兴趣可以自己研究下