久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

Java 微信公眾號(hào)開發(fā)相關(guān)總結(jié)

瀏覽:150日期:2022-05-22 10:59:54
目錄首先必須要有一個(gè)個(gè)人微信公眾號(hào)效果圖后臺(tái)路由代碼完整代碼首先必須要有一個(gè)個(gè)人微信公眾號(hào)

個(gè)人微信公眾號(hào)相關(guān)的接口權(quán)限有限,不過用于個(gè)人學(xué)習(xí)體驗(yàn)一下足夠了,如圖:

Java 微信公眾號(hào)開發(fā)相關(guān)總結(jié)

然后進(jìn)入微信公眾后臺(tái),點(diǎn)擊基本配置,按照如下操作(點(diǎn)擊啟用,相當(dāng)于設(shè)置請求url為自己后臺(tái)的):

Java 微信公眾號(hào)開發(fā)相關(guān)總結(jié)

設(shè)置服務(wù)器URL、令牌、消息加解密密鑰(這個(gè)可以使用自動(dòng)生成的):

Java 微信公眾號(hào)開發(fā)相關(guān)總結(jié)

服務(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)。

效果圖

Java 微信公眾號(hào)開發(fā)相關(guān)總結(jié)

上面的演示效果來自本人微信公眾號(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)文章!

標(biāo)簽: 微信
相關(guān)文章:
主站蜘蛛池模板: 镇巴县| 古浪县| 西华县| 鞍山市| 金堂县| 景泰县| 逊克县| 沂源县| 福建省| 保定市| 柯坪县| 新邵县| 兰溪市| 三穗县| 江源县| 军事| 叙永县| 突泉县| 萨嘎县| 新昌县| 泰兴市| 贵定县| 内江市| 阿合奇县| 临泉县| 九江县| 兴宁市| 黄陵县| 陕西省| 江门市| 三门峡市| 华亭县| 衡南县| 都昌县| 和田县| 沙坪坝区| 溧水县| 苗栗市| 南城县| 甘谷县| 双辽市|