Java 微信公眾號開發(fā)相關(guān)總結(jié)
首先必須要有一個個人微信公眾號
個人微信公眾號相關(guān)的接口權(quán)限有限,不過用于個人學(xué)習(xí)體驗一下足夠了,如圖:
然后進(jìn)入微信公眾后臺,點擊基本配置,按照如下操作(點擊啟用,相當(dāng)于設(shè)置請求url為自己后臺的):
設(shè)置服務(wù)器URL、令牌、消息加解密密鑰(這個可以使用自動生成的):
服務(wù)器URL至關(guān)重要,我在這里設(shè)置為我自己的域名http://www.youcongtech.com/wx-api。
這個wx-api就是后面對應(yīng)的接口(比如我發(fā)送某個關(guān)鍵字,返回對應(yīng)的信息)。
token可以設(shè)置復(fù)雜點。
效果圖
上面的演示效果來自本人微信公眾號,并長期運行穩(wěn)定沒有任何問題。
后臺路由代碼
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; /** * 微信公眾號API * @author youcong * @date 2019-6-02 */ @RestController @RequestMapping("/wx_public_api") @Api(tags = { "微信公眾號api" }, description = "微信公眾號api") public class WxPublicApiController extends AbstractController{ @Autowired private WxService wxService; /** * 微信公眾平臺服務(wù)器配置驗證 * @param request * @param response */ @GetMapping @ApiOperation("微信公眾平臺服務(wù)器配置驗證") public void validate(HttpServletRequest request, HttpServletResponse response) { // 微信加密簽名,signature結(jié)合了開發(fā)者填寫的token參數(shù)和請求中的timestamp參數(shù)、nonce參數(shù)。 String signature = request.getParameter("signature"); // 時間戳 String timestamp = request.getParameter("timestamp"); // 隨機數(shù) String nonce = request.getParameter("nonce"); // 隨機字符串 String echostr = request.getParameter("echostr"); PrintWriter out = null; try { out = response.getWriter(); // 通過檢驗signature對請求進(jì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)放到我個人的GitHub倉庫,地址為:https://github.com/developers-youcong/blog-springcloud-pro/tree/master/blog-wx-client
這是其中的子項目,功能主要是微信公眾平臺。
鑒于我個人主要維護(hù)的開源項目尚未公開,有很多隱私信息等,所以將其中的微信公眾號模塊抽取出來放到我的新開源項目blog-springcloud-pro中(此項目目前處于開發(fā)中)。
微信公眾號模塊基本上換上自己的token、appid、appsecret并部署到線上就基本可用了。有任何問題,可留言。
以上就是Java 微信公眾號開發(fā)相關(guān)總結(jié)的詳細(xì)內(nèi)容,更多關(guān)于Java 微信公眾號開發(fā)的資料請關(guān)注腳本之家其它相關(guān)文章!
- java微信公眾號企業(yè)付款開發(fā)
- Java版微信公眾號支付開發(fā)全過程
- 微信公眾號開發(fā)之設(shè)置自定義菜單實例代碼【java版】
- java微信公眾號支付開發(fā)之現(xiàn)金紅包
- 微信公眾號開發(fā)之回復(fù)圖文消息java代碼
- Java微信公眾號開發(fā)之通過微信公眾號獲取用戶信息
- java微信公眾號開發(fā)案例
- 用Java設(shè)計模式中的觀察者模式開發(fā)微信公眾號的例子
- java微信公眾號開發(fā)第一步 公眾號接入和access_token管理
- Java開發(fā)微信公眾號接收和被動回復(fù)普通消息
- java微信公眾號開發(fā)(搭建本地測試環(huán)境)
- java開發(fā)微信公眾號支付
相關(guān)文章
如何使用@ConditionalOnExpression決定是否生效注釋
這篇文章主要介紹了如何使用@ConditionalOnExpression決定是否生效注釋的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06利用feign調(diào)用返回object類型轉(zhuǎn)換成實體
這篇文章主要介紹了利用feign調(diào)用返回object類型轉(zhuǎn)換成實體,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03Java和C++通過new創(chuàng)建的對象有何區(qū)別?
Java和C++都是面向?qū)ο蟮木幊陶Z言,然而Java和C++在創(chuàng)建對象時卻存在不同的方式,由于方式的不同導(dǎo)致在內(nèi)存中管理的不同。這篇文章主要給大家介紹了關(guān)于Java和C++通過new創(chuàng)建對象區(qū)別的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-11-11