java微信公众号开发一:服务器信息配置
生活随笔
收集整理的這篇文章主要介紹了
java微信公众号开发一:服务器信息配置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
假設你已經擁有一個微信測試號,基本信息如下圖所示:
編輯扣接口配置信息,URL為域名服務信息地址/項目名/controller中配置的requestmapping路徑(個人開發測試的時候是用的NATAPP進行的內網穿透生成的訪問路徑,本人計算機基礎比較差,說實話我還是不太理解什么原理,大神可以在下面指導一下)
接下來在java項目中編寫入口controller,get請求用來接收微信服務器發送的驗證請求,post用來接收微信端發過來的消息請求
package com.zhoumin.wechat.controller;import com.zhoumin.wechat.service.MessageService; import com.zhoumin.wechat.utils.SignUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException;@RestController @RequestMapping("/wechatEntrance") public class WechatIndexController {private static final Logger logger = LoggerFactory.getLogger(WechatIndexController.class);@Autowiredprivate MessageService messageService;@RequestMapping(method = RequestMethod.GET)public void get(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) {logger.error("IO異常",e.getMessage());} finally {out.close();}}@RequestMapping(method = RequestMethod.POST)public void post(HttpServletRequest request, HttpServletResponse response) {try {request.setCharacterEncoding("UTF-8");} catch (UnsupportedEncodingException e) {e.printStackTrace();}response.setCharacterEncoding("UTF-8");// 調用核心業務類接收消息、處理消息String respMessage = messageService.newMessageRequest(request);// 響應消息PrintWriter out = null;try {out = response.getWriter();out.print(respMessage);} catch (IOException e) {logger.error("IO異常",e.getMessage());} finally {out.close();}}}以上搞好之后,點擊接口配置提交按鈕,顯示配置成功就表示測試號已經配置好后臺服務信息了,如果失敗的話就要參考微信開發文檔找找原因,筆者遇到基本上是服務器信息URL配置的問題。
總結
以上是生活随笔為你收集整理的java微信公众号开发一:服务器信息配置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 防范于未“燃”|涂鸦智慧社区推出“黑科技
- 下一篇: 微软危急: 20年转型未果 复兴路上最大