Java 微信公眾號(hào)開發(fā)相關(guān)總結(jié)
個(gè)人微信公眾號(hào)相關(guān)的接口權(quán)限有限,不過用于個(gè)人學(xué)習(xí)體驗(yàn)一下足夠了,如圖:
然后進(jìn)入微信公眾后臺(tái),點(diǎn)擊基本配置,按照如下操作(點(diǎn)擊啟用,相當(dāng)于設(shè)置請求url為自己后臺(tái)的):
設(shè)置服務(wù)器URL、令牌、消息加解密密鑰(這個(gè)可以使用自動(dòng)生成的):
服務(wù)器URL至關(guān)重要,我在這里設(shè)置為我自己的域名http://www.youcongtech.com/wx-api。
這個(gè)wx-api就是后面對應(yīng)的接口(比如我發(fā)送某個(gè)關(guān)鍵字,返回對應(yīng)的信息)。token可以設(shè)置復(fù)雜點(diǎn)。
效果圖上面的演示效果來自本人微信公眾號(hào),并長期運(yùn)行穩(wěn)定沒有任何問題。
后臺(tái)路由代碼package com.blog.springboot.controller;import java.io.IOException;import java.io.PrintWriter;import java.io.UnsupportedEncodingException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import com.blog.springboot.wx.service.WxService;import com.blog.springboot.wx.util.SignUtil;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;/** * 微信公眾號(hào)API * @author youcong * @date 2019-6-02 */@RestController@RequestMapping('/wx_public_api')@Api(tags = { '微信公眾號(hào)api' }, description = '微信公眾號(hào)api')public class WxPublicApiController extends AbstractController{@Autowiredprivate WxService wxService; /*** 微信公眾平臺(tái)服務(wù)器配置驗(yàn)證* @param request* @param response*/ @GetMapping @ApiOperation('微信公眾平臺(tái)服務(wù)器配置驗(yàn)證') public void validate(HttpServletRequest request, HttpServletResponse response) {// 微信加密簽名,signature結(jié)合了開發(fā)者填寫的token參數(shù)和請求中的timestamp參數(shù)、nonce參數(shù)。String signature = request.getParameter('signature');// 時(shí)間戳String timestamp = request.getParameter('timestamp');// 隨機(jī)數(shù)String nonce = request.getParameter('nonce');// 隨機(jī)字符串String echostr = request.getParameter('echostr');PrintWriter out = null;try { out = response.getWriter(); // 通過檢驗(yàn)signature對請求進(jìn)行校驗(yàn),若校驗(yàn)成功則原樣返回echostr,否則接入失敗 if (SignUtil.checkSignature(signature, timestamp, nonce)) {out.print(echostr); }} catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage()); } finally { out.close(); out = null;} } /** * 關(guān)注推送消息 * @param request * @param response */ @PostMapping @ApiOperation('關(guān)注推送消息') public void about(HttpServletRequest request, HttpServletResponse response) {try { request.setCharacterEncoding('UTF-8');} catch (UnsupportedEncodingException e) { e.printStackTrace(); logger.error(e.getMessage(),e);}response.setContentType('text/html;charset=UTF-8');// 調(diào)用核心業(yè)務(wù)類接收消息、處理消息String respMessage = wxService.newMessageRequest(request);// 響應(yīng)消息PrintWriter out = null;try { out = response.getWriter(); out.print(respMessage);} catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage(),e);} finally { out.close(); out = null;} }}完整代碼
完整代碼已經(jīng)放到我個(gè)人的GitHub倉庫,地址為:https://github.com/developers-youcong/blog-springcloud-pro/tree/master/blog-wx-client
這是其中的子項(xiàng)目,功能主要是微信公眾平臺(tái)。
鑒于我個(gè)人主要維護(hù)的開源項(xiàng)目尚未公開,有很多隱私信息等,所以將其中的微信公眾號(hào)模塊抽取出來放到我的新開源項(xiàng)目blog-springcloud-pro中(此項(xiàng)目目前處于開發(fā)中)。
微信公眾號(hào)模塊基本上換上自己的token、appid、appsecret并部署到線上就基本可用了。有任何問題,可留言。
以上就是Java 微信公眾號(hào)開發(fā)相關(guān)總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Java 微信公眾號(hào)開發(fā)的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 微信輔助解封號(hào)碼填錯(cuò)了怎么改過來2. 鐵路12306中找回密碼的詳細(xì)步驟3. 釘釘加入了群為什么不顯示4. 拼多多APP如何通過圖片搜索?通過圖片搜索的方法一覽5. 如何將釘釘運(yùn)動(dòng)關(guān)掉?關(guān)閉釘釘運(yùn)動(dòng)的方法講解6. 華為watchfitnew怎么設(shè)置微信提醒7. 微博瀏覽記錄怎么刪除8. 快手通訊錄好友怎么刪除9. 在華為mate20 RS中怎么設(shè)置支付寶刷臉支付?支付寶刷臉支付設(shè)置方法講解10. 電腦版釘釘如何設(shè)置英文或繁體中文?釘釘電腦版語言修改方法詳解
