這篇文章將為大家詳細講解有關(guān)利用java開發(fā)微信公眾號的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
專注于為中小企業(yè)提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、外貿(mào)營銷網(wǎng)站建設(shè)服務(wù),電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)南譙免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了上1000家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴充和轉(zhuǎn)變。
下面我們就來簡單的描述下接入公眾號的步驟:
1、首先你需要一個郵箱在微信公眾號平臺進行注冊;
注冊的方式有訂閱號、公眾號、小程序和企業(yè)號,個人我們這里只能選擇訂閱號
2、注冊完后,我們登錄到公眾號平臺--->開發(fā)--->基本配置,這里需要填寫URL和token,URL就是我們使用服務(wù)器的接口;
3、Java Web服務(wù)器程序編譯好且在服務(wù)器上部署可以運行的話,可在微信公眾號進行在線接口調(diào)試:
1)、選擇合適的接口
2)、系統(tǒng)會生成該接口的參數(shù)表,您可以直接在文本框內(nèi)填入對應(yīng)的參數(shù)值(紅色星號表示該字段必填)
3)、點擊檢查問題按鈕,即可得到相應(yīng)的調(diào)試信息
eg:獲取access_token的步驟:
1)、接口類型:基礎(chǔ)支持
2)、接口列表:獲取access_token接口/token
3)、填寫相應(yīng)的參數(shù):grant_type、appid、secret
4)、點擊檢查問題
5)、驗證成功會返回結(jié)果和提示,結(jié)果為:200 ok,提示:Request successful即表示驗證成功
我們這里驗證比較多的是消息接口調(diào)試:文本消息、圖片消息、語音消息、視頻消息、etc
4、接口有不理解的地方,可進入開發(fā)-->開發(fā)者工具-->開發(fā)者文檔進行查詢
5、接口權(quán)限:訂閱號主要有基礎(chǔ)支持、接收消息及網(wǎng)頁服務(wù)里面的一些接口
下面我們主要講訂閱號怎么樣接收消息的案例:
1、需要申請一個個人微信訂閱號
2、url服務(wù)器和服務(wù)器端代碼部署(可以用騰訊云or阿里云服務(wù)器)
1)、AccountsServlet.java類,驗證來自微信服務(wù)器和微信服務(wù)器的消息處理
package cn.jon.wechat.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import cn.jon.wechat.service.AccountsService; import cn.jon.wechat.utils.SignUtil; public class AccountsServlet extends HttpServlet { public AccountsServlet() { super(); } public void destroy() { super.destroy(); // Put your code here } /** * 確認請求來自于微信服務(wù)器 */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("接口測試開始?。?!"); //微信加密簽名 String signature = request.getParameter("signature"); //時間戳 String timestamp = request.getParameter("timestamp"); //隨機數(shù) String nonce = request.getParameter("nonce"); //隨機字符串 String echostr = request.getParameter("echostr"); PrintWriter out = response.getWriter(); //通過校驗signature對請求進行校驗,若校驗成功則原樣返回echostr,表示接入成功,否則接入失敗 if(SignUtil.checkSignature(signature,timestamp,nonce)){ out.print(echostr); } out.close(); out = null; // response.encodeRedirectURL("success.jsp"); } /** * 處理微信服務(wù)器發(fā)來的消息 */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //消息的接受、處理、響應(yīng) request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); //調(diào)用核心業(yè)務(wù)類型接受消息、處理消息 String respMessage = AccountsService.processRequest(request); //響應(yīng)消息 PrintWriter out = response.getWriter(); out.print(respMessage); out.close(); } public void init() throws ServletException { // Put your code here } }
2)、SignUtil.java類,請求校驗工具類,token需要和微信中填寫的token一致
package cn.jon.wechat.utils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; /** * 請求校驗工具類 * @author jon */ public class SignUtil { //與微信配置中的的Token一致 private static String token = ""; public static boolean checkSignature(String signature, String timestamp, String nonce) { String[] arra = new String[]{token,timestamp,nonce}; //將signature,timestamp,nonce組成數(shù)組進行字典排序 Arrays.sort(arra); StringBuilder sb = new StringBuilder(); for(int i=0;i>>4)&0X0F]; tmpArr[1] = Digit[dByte&0X0F]; String s = new String(tmpArr); return s; } public static void main(String[] args) { /*byte dByte = 'A'; System.out.println(byteToHexStr(dByte));*/ Map map = new ConcurrentHashMap (); map.put("4", "zhangsan"); map.put("100", "lisi"); Set set = map.keySet(); Iterator iter = set.iterator(); while(iter.hasNext()){ // String keyV = (String) iter.next(); String key =(String)iter.next(); System.out.println(map.get(key)); // System.out.println(map.get(iter.next())); } /*for(int i=0;i 3)、AccountsService.java服務(wù)類,主要是消息的請求和響應(yīng)處理,并且當用戶關(guān)注你的公眾號的時候,可以設(shè)置默認推送消息
package cn.jon.wechat.service; import java.util.Date; import java.util.Map; import javax.servlet.http.HttpServletRequest; import cn.jon.wechat.message.req.ImageMessage; import cn.jon.wechat.message.req.LinkMessage; import cn.jon.wechat.message.req.LocationMessage; import cn.jon.wechat.message.req.VideoMessage; import cn.jon.wechat.message.req.VoiceMessage; import cn.jon.wechat.message.resp.TextMessage; import cn.jon.wechat.utils.MessageUtil; /** * 解耦,使控制層與業(yè)務(wù)邏輯層分離開來,主要處理請求,響應(yīng) * @author jon */ public class AccountsService { public static String processRequest(HttpServletRequest request) { String respMessage = null; //默認返回的文本消息內(nèi)容 String respContent = "請求處理異常,請稍后嘗試!"; try { //xml請求解析 MaprequestMap = MessageUtil.pareXml(request); //發(fā)送方賬號(open_id) String fromUserName = requestMap.get("FromUserName"); //公眾賬號 String toUserName = requestMap.get("ToUserName"); //消息類型 String msgType = requestMap.get("MsgType"); //默認回復(fù)此文本消息 TextMessage defaultTextMessage = new TextMessage(); defaultTextMessage.setToUserName(fromUserName); defaultTextMessage.setFromUserName(toUserName); defaultTextMessage.setCreateTime(new Date().getTime()); defaultTextMessage.setMsgType(MessageUtil.MESSSAGE_TYPE_TEXT); defaultTextMessage.setFuncFlag(0); // 由于href屬性值必須用雙引號引起,這與字符串本身的雙引號沖突,所以要轉(zhuǎn)義 defaultTextMessage.setContent("歡迎訪問jon的博客!"); // defaultTextMessage.setContent(getMainMenu()); // 將文本消息對象轉(zhuǎn)換成xml字符串 respMessage = MessageUtil.textMessageToXml(defaultTextMessage); //文本消息 if(msgType.equals(MessageUtil.MESSSAGE_TYPE_TEXT)){ //respContent = "Hi,您發(fā)送的是文本消息!"; //回復(fù)文本消息 TextMessage textMessage = new TextMessage(); // textMessage.setToUserName(toUserName); // textMessage.setFromUserName(fromUserName); //這里需要注意,否則無法回復(fù)消息給用戶了 textMessage.setToUserName(fromUserName); textMessage.setFromUserName(toUserName); textMessage.setCreateTime(new Date().getTime()); textMessage.setMsgType(MessageUtil.MESSSAGE_TYPE_TEXT); textMessage.setFuncFlag(0); respContent = "Hi,你發(fā)的消息是:"+requestMap.get("Content"); textMessage.setContent(respContent); respMessage = MessageUtil.textMessageToXml(textMessage); } //圖片消息 else if(msgType.equals(MessageUtil.MESSSAGE_TYPE_IMAGE)){ ImageMessage imageMessage=new ImageMessage(); imageMessage.setToUserName(fromUserName); imageMessage.setFromUserName(toUserName); imageMessage.setCreateTime(new Date().getTime()); imageMessage.setMsgType(MessageUtil.MESSSAGE_TYPE_IMAGE); //respContent=requestMap.get("PicUrl"); imageMessage.setPicUrl("/upload/otherpica41/11692.jpg"); respMessage = MessageUtil.imageMessageToXml(imageMessage); } //地理位置 else if(msgType.equals(MessageUtil.MESSSAGE_TYPE_LOCATION)){ LocationMessage locationMessage=new LocationMessage(); locationMessage.setToUserName(fromUserName); locationMessage.setFromUserName(toUserName); locationMessage.setCreateTime(new Date().getTime()); locationMessage.setMsgType(MessageUtil.MESSSAGE_TYPE_LOCATION); locationMessage.setLocation_X(requestMap.get("Location_X")); locationMessage.setLocation_Y(requestMap.get("Location_Y")); locationMessage.setScale(requestMap.get("Scale")); locationMessage.setLabel(requestMap.get("Label")); respMessage = MessageUtil.locationMessageToXml(locationMessage); } //視頻消息 else if(msgType.equals(MessageUtil.MESSSAGE_TYPE_VIDEO)){ VideoMessage videoMessage=new VideoMessage(); videoMessage.setToUserName(fromUserName); videoMessage.setFromUserName(toUserName); videoMessage.setCreateTime(new Date().getTime()); videoMessage.setMsgType(MessageUtil.MESSSAGE_TYPE_VIDEO); videoMessage.setMediaId(requestMap.get("MediaId")); videoMessage.setThumbMediaId(requestMap.get("ThumbMediaId")); respMessage = MessageUtil.videoMessageToXml(videoMessage); } //鏈接消息 else if(msgType.equals(MessageUtil.MESSSAGE_TYPE_LINK)){ LinkMessage linkMessage=new LinkMessage(); linkMessage.setToUserName(fromUserName); linkMessage.setFromUserName(toUserName); linkMessage.setCreateTime(new Date().getTime()); linkMessage.setMsgType(MessageUtil.MESSSAGE_TYPE_LINK); linkMessage.setTitle(requestMap.get("Title")); linkMessage.setDescription(requestMap.get("Description")); linkMessage.setUrl(requestMap.get("Url")); respMessage = MessageUtil.linkMessageToXml(linkMessage); } //語音消息 else if(msgType.equals(MessageUtil.MESSSAGE_TYPE_VOICE)){ VoiceMessage voiceMessage=new VoiceMessage(); voiceMessage.setToUserName(fromUserName); voiceMessage.setFromUserName(toUserName); voiceMessage.setCreateTime(new Date().getTime()); voiceMessage.setMsgType(MessageUtil.MESSSAGE_TYPE_VOICE); voiceMessage.setMediaId(requestMap.get("MediaId")); voiceMessage.setFormat(requestMap.get("Format")); respMessage = MessageUtil.voiceMessageToXml(voiceMessage); } //事件推送 else if(msgType.equals(MessageUtil.MESSSAGE_TYPE_EVENT)){ //事件類型 String eventType = requestMap.get("Event"); //訂閱 if(eventType.equals(MessageUtil.EVENT_TYPE_SUBSCRIBE)){ respContent = "謝謝關(guān)注!"; } //取消訂閱 else if(eventType.equals(MessageUtil.EVENT_TYPE_UNSUBSCRIBE)){ // System.out.println("取消訂閱"); } else if(eventType.equals(MessageUtil.EVENT_TYPE_CLICK)){ //自定義菜單消息處理 System.out.println("自定義菜單消息處理"); } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return respMessage; } public static String getMainMenu() { StringBuffer buffer =new StringBuffer(); buffer.append("您好,我是jon,請回復(fù)數(shù)字選擇服務(wù):").append("\n"); buffer.append("1、我的博客").append("\n"); buffer.append("2、 歌曲點播").append("\n"); buffer.append("3、 經(jīng)典游戲").append("\n"); buffer.append("4 、聊天打牌").append("\n\n"); buffer.append("回復(fù)"+"0"+"顯示幫助菜單"); return buffer.toString(); } } 4)、MessageUtil.java幫助類,包括常量的定義和xml消息轉(zhuǎn)換和處理
package cn.jon.wechat.utils; import java.io.InputStream; import java.io.Writer; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; import cn.jon.wechat.message.req.ImageMessage; import cn.jon.wechat.message.req.LinkMessage; import cn.jon.wechat.message.req.LocationMessage; import cn.jon.wechat.message.req.VideoMessage; import cn.jon.wechat.message.req.VoiceMessage; import cn.jon.wechat.message.resp.Article; import cn.jon.wechat.message.resp.MusicMessage; import cn.jon.wechat.message.resp.NewsMessage; import cn.jon.wechat.message.resp.TextMessage; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.core.util.QuickWriter; import com.thoughtworks.xstream.io.HierarchicalStreamWriter; import com.thoughtworks.xstream.io.xml.PrettyPrintWriter; import com.thoughtworks.xstream.io.xml.XppDriver; /** * 各種消息的處理類 * @author jon */ public class MessageUtil { /** * 文本類型 */ public static final String MESSSAGE_TYPE_TEXT = "text"; /** * 音樂類型 */ public static final String MESSSAGE_TYPE_MUSIC = "music"; /** * 圖文類型 */ public static final String MESSSAGE_TYPE_NEWS = "news"; /** * 視頻類型 */ public static final String MESSSAGE_TYPE_VIDEO = "video"; /** * 圖片類型 */ public static final String MESSSAGE_TYPE_IMAGE = "image"; /** * 鏈接類型 */ public static final String MESSSAGE_TYPE_LINK = "link"; /** * 地理位置類型 */ public static final String MESSSAGE_TYPE_LOCATION = "location"; /** * 音頻類型 */ public static final String MESSSAGE_TYPE_VOICE = "voice"; /** * 推送類型 */ public static final String MESSSAGE_TYPE_EVENT = "event"; /** * 事件類型:subscribe(訂閱) */ public static final String EVENT_TYPE_SUBSCRIBE = "subscribe"; /** * 事件類型:unsubscribe(取消訂閱) */ public static final String EVENT_TYPE_UNSUBSCRIBE = "unsubscribe"; /** * 事件類型:click(自定義菜單點擊事件) */ public static final String EVENT_TYPE_CLICK= "CLICK"; /** * 解析微信發(fā)來的請求 XML */ @SuppressWarnings("unchecked") public static MappareXml(HttpServletRequest request) throws Exception { //將解析的結(jié)果存儲在HashMap中 Map reqMap = new HashMap (); //從request中取得輸入流 InputStream inputStream = request.getInputStream(); //讀取輸入流 SAXReader reader = new SAXReader(); Document document = reader.read(inputStream); //得到xml根元素 Element root = document.getRootElement(); //得到根元素的所有子節(jié)點 List elementList = root.elements(); //遍歷所有的子節(jié)點取得信息類容 for(Element elem:elementList){ reqMap.put(elem.getName(),elem.getText()); } //釋放資源 inputStream.close(); inputStream = null; return reqMap; } /** * 響應(yīng)消息轉(zhuǎn)換成xml返回 * 文本對象轉(zhuǎn)換成xml */ public static String textMessageToXml(TextMessage textMessage) { xstream.alias("xml", textMessage.getClass()); return xstream.toXML(textMessage); } /** * 語音對象的轉(zhuǎn)換成xml * */ public static String voiceMessageToXml(VoiceMessage voiceMessage) { xstream.alias("xml", voiceMessage.getClass()); return xstream.toXML(voiceMessage); } /** * 視頻對象的轉(zhuǎn)換成xml * */ public static String videoMessageToXml(VideoMessage videoMessage) { xstream.alias("xml", videoMessage.getClass()); return xstream.toXML(videoMessage); } /** * 音樂對象的轉(zhuǎn)換成xml * */ public static String musicMessageToXml(MusicMessage musicMessage) { xstream.alias("xml", musicMessage.getClass()); return xstream.toXML(musicMessage); } /** * 圖文對象轉(zhuǎn)換成xml * */ public static String newsMessageToXml(NewsMessage newsMessage) { xstream.alias("xml", newsMessage.getClass()); xstream.alias("item", new Article().getClass()); return xstream.toXML(newsMessage); } /** * 圖片對象轉(zhuǎn)換成xml * */ public static String imageMessageToXml(ImageMessage imageMessage) { xstream.alias("xml",imageMessage.getClass()); return xstream.toXML(imageMessage); } /** * 鏈接對象轉(zhuǎn)換成xml * */ public static String linkMessageToXml(LinkMessage linkMessage) { xstream.alias("xml",linkMessage.getClass()); return xstream.toXML(linkMessage); } /** * 地理位置對象轉(zhuǎn)換成xml * */ public static String locationMessageToXml(LocationMessage locationMessage) { xstream.alias("xml",locationMessage.getClass()); return xstream.toXML(locationMessage); } /** * 拓展xstream,使得支持CDATA塊 * */ private static XStream xstream = new XStream(new XppDriver(){ public HierarchicalStreamWriter createWriter(Writer out){ return new PrettyPrintWriter(out){ //對所有的xml節(jié)點的轉(zhuǎn)換都增加CDATA標記 boolean cdata = true; @SuppressWarnings("unchecked") public void startNode(String name,Class clazz){ super.startNode(name,clazz); } protected void writeText(QuickWriter writer,String text){ if(cdata){ writer.write(""); }else{ writer.write(text); } } }; } }); } 5)、BaseMessage.java消息基類(包括:開發(fā)者微信號、用戶賬戶、創(chuàng)建時間、消息類型、消息ID變量),文本、視頻、圖片消息都會繼承此基類,在此基礎(chǔ)上擴展自己的變量,可根據(jù)開發(fā)者文檔中的各種消息屬性進行定義
package cn.jon.wechat.message.req; /** * 消息基類 (普通用戶-公眾號) * @author jon */ public class BaseMessage { //開發(fā)者微信號 private String ToUserName; //發(fā)送方賬號(一個openId) private String FromUserName; //消息創(chuàng)建時間(整型) private long CreateTime; //消息類型(text/image/location/link...) private String MsgType; //消息id 64位整型 private String MsgId; public BaseMessage() { super(); // TODO Auto-generated constructor stub } public BaseMessage(String toUserName, String fromUserName, long createTime, String msgType, String msgId) { super(); ToUserName = toUserName; FromUserName = fromUserName; CreateTime = createTime; MsgType = msgType; MsgId = msgId; } public String getToUserName() { return ToUserName; } public void setToUserName(String toUserName) { ToUserName = toUserName; } public String getFromUserName() { return FromUserName; } public void setFromUserName(String fromUserName) { FromUserName = fromUserName; } public long getCreateTime() { return CreateTime; } public void setCreateTime(long createTime) { CreateTime = createTime; } public String getMsgType() { return MsgType; } public void setMsgType(String msgType) { MsgType = msgType; } public String getMsgId() { return MsgId; } public void setMsgId(String msgId) { MsgId = msgId; } }6)、TextMessage.java文本消息,繼承自5中基類,擴展內(nèi)容屬性
package cn.jon.wechat.message.req; /** * 文本消息 * @author jon */ public class TextMessage extends BaseMessage{ //消息內(nèi)容 private String content; public String getContent() { return content; } public void setContent(String content) { this.content = content; } }7)、ImageMessage.java圖片消息
package cn.jon.wechat.message.req; /** * 圖片消息 * @author jon */ public class ImageMessage extends BaseMessage{ //圖片鏈接 private String PicUrl; public String getPicUrl() { return PicUrl; } public void setPicUrl(String picUrl) { PicUrl = picUrl; } }8)、VideoMessage.java視頻消息
package cn.jon.wechat.message.req; public class VideoMessage extends BaseMessage { private String mediaId; private String thumbMediaId; public String getMediaId() { return mediaId; } public void setMediaId(String mediaId) { this.mediaId = mediaId; } public String getThumbMediaId() { return thumbMediaId; } public void setThumbMediaId(String thumbMediaId) { this.thumbMediaId = thumbMediaId; } }關(guān)于“利用java開發(fā)微信公眾號的方法”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
本文名稱:利用java開發(fā)微信公眾號的方法
文章起源:http://weahome.cn/article/ispodg.html