溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何使用Java開發(fā)微信公眾號

發(fā)布時(shí)間:2021-05-25 15:24:56 來源:億速云 閱讀:277 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章給大家介紹如何使用Java開發(fā)微信公眾號,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

首先必須要有一個(gè)個(gè)人微信公眾號

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

如何使用Java開發(fā)微信公眾號

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

如何使用Java開發(fā)微信公眾號

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

如何使用Java開發(fā)微信公眾號

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

后臺(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;

@RestController
@RequestMapping("/wx_public_api")
@Api(tags = { "微信公眾號api" }, description = "微信公眾號api")
public class WxPublicApiController extends AbstractController{

	@Autowired
	private 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;
	        }
	    }
}

java基本數(shù)據(jù)類型有哪些

Java的基本數(shù)據(jù)類型分為:1、整數(shù)類型,用來表示整數(shù)的數(shù)據(jù)類型。2、浮點(diǎn)類型,用來表示小數(shù)的數(shù)據(jù)類型。3、字符類型,字符類型的關(guān)鍵字是“char”。4、布爾類型,是表示邏輯值的基本數(shù)據(jù)類型。

關(guān)于如何使用Java開發(fā)微信公眾號就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI