此次以文本回復做爲案例來說解Java相關得微信公衆號開發。html
我的微信公衆號相關的接口權限有限,不過用於我的學習體驗一下足夠了,如圖:java
而後進入微信公衆後臺,點擊基本配置,按照以下操做(點擊啓用,至關於設置請求url爲本身後臺的):git
設置服務器URL、令牌、消息加解密密鑰(這個可使用自動生成的):github
服務器URL相當重要,我在這裏設置爲我本身的域名http://www.youcongtech.com/wx-api。web
這個wx-api就是後面對應的接口(好比我發送某個關鍵字,返回對應的信息)。
token能夠設置複雜點。spring
上面的演示效果來自本人微信公衆號,並長期運行穩定沒有任何問題api
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; /** * 微信公衆平臺服務器配置驗證 * @param request * @param response */ @GetMapping @ApiOperation("微信公衆平臺服務器配置驗證") public void validate(HttpServletRequest request, HttpServletResponse response) { // 微信加密簽名,signature結合了開發者填寫的token參數和請求中的timestamp參數、nonce參數。 String signature = request.getParameter("signature"); // 時間戳 String timestamp = request.getParameter("timestamp"); // 隨機數 String nonce = request.getParameter("nonce"); // 隨機字符串 String echostr = request.getParameter("echostr"); PrintWriter out = null; try { out = response.getWriter(); // 經過檢驗signature對請求進行校驗,若校驗成功則原樣返回echostr,不然接入失敗 if (SignUtil.checkSignature(signature, timestamp, nonce)) { out.print(echostr); } } catch (IOException e) { e.printStackTrace(); logger.error(e.getMessage()); } finally { out.close(); out = null; } } /** * 關注推送消息 * @param request * @param response */ @PostMapping @ApiOperation("關注推送消息") 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"); // 調用核心業務類接收消息、處理消息 String respMessage = wxService.newMessageRequest(request); // 響應消息 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; } } }
完整代碼已經放到我我的的GitHub倉庫,地址爲:https://github.com/developers-youcong/blog-springcloud-pro/tree/master/blog-wx-clientspringboot
這是其中的子項目,功能主要是微信公衆平臺。服務器
鑑於我我的主要維護的開源項目還沒有公開,有不少隱私信息等,因此將其中的微信公衆號模塊抽取出來放到個人新開源項目blog-springcloud-pro中(此項目目前處於開發中)。微信
微信公衆號模塊基本上換上本身的token、appid、appsecret並部署到線上就基本可用了。有任何問題,可留言。