SpringBoot小程序推送信息的項(xiàng)目實(shí)踐
1.小程序推送信息列如我們?nèi)ゲ蛷d等位有預(yù)約提醒,剩余桌數(shù)
首先申請一個(gè)小程序,微信開放平臺(tái):小程序
2.申請小程序信息,申請信息模板
appid
AppSecret
3.根據(jù)開發(fā)文檔開發(fā)
subscribeMessage.send | 微信開放文檔
4.代碼如下:
引入依賴
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency>
先準(zhǔn)備一個(gè)HTTP工具類Z
import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.ssl.NoopHostnameVerifier; import org.apache.http.conn.ssl.SSLConnectionSocketFactory; import org.apache.http.conn.ssl.TrustSelfSignedStrategy; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.ssl.SSLContexts; import org.apache.http.util.EntityUtils; import java.io.IOException; /** * @author lrx * @description: TODO * @date 2021/3/9 9:50 */ public class HttpClientUtils { //Http協(xié)議GET請求 public static String httpGet(String url) throws IOException { //初始化HttpClient CloseableHttpClient httpClient = HttpClients.createDefault(); //創(chuàng)建HttpGet HttpGet httpGet = new HttpGet(url); //發(fā)起請求,獲取response對象 CloseableHttpResponse response = httpClient.execute(httpGet); //獲取請求狀態(tài)碼 //response.getStatusLine().getStatusCode(); //獲取返回?cái)?shù)據(jù)實(shí)體對象 HttpEntity entity = response.getEntity(); //轉(zhuǎn)為字符串 String result = EntityUtils.toString(entity, "UTF-8"); return result; } //Http協(xié)議Post請求 public static String httpPost(String url, String json) throws Exception { //初始HttpClient CloseableHttpClient httpClient = HttpClients.createDefault(); //創(chuàng)建Post對象 HttpPost httpPost = new HttpPost(url); //設(shè)置Content-Type /* httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");*/ StringEntity se = new StringEntity(json,"UTF-8"); se.setContentType("application/x-www-form-urlencoded"); httpPost.setEntity(se); //發(fā)起請求,獲取response對象 CloseableHttpResponse response = httpClient.execute(httpPost); //獲取請求碼 //response.getStatusLine().getStatusCode(); //獲取返回?cái)?shù)據(jù)實(shí)體對象 HttpEntity entity = response.getEntity(); //轉(zhuǎn)為字符串 String result = EntityUtils.toString(entity, "UTF-8"); return result; } //Https協(xié)議Get請求 public static String httpsGet(String url) throws Exception { CloseableHttpClient hp = createSSLClientDefault(); HttpGet hg = new HttpGet(url); CloseableHttpResponse response = hp.execute(hg); HttpEntity entity = response.getEntity(); String content = EntityUtils.toString(entity, "UTF-8"); hp.close(); return content; } //Https協(xié)議Post請求 public static String httpsPost(String url, String json) throws Exception { CloseableHttpClient hp = createSSLClientDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("Content-Type", "application/json"); httpPost.setEntity(new StringEntity(json)); CloseableHttpResponse response = hp.execute(httpPost); HttpEntity entity = response.getEntity(); String content = EntityUtils.toString(entity, "UTF-8"); hp.close(); return content; } public static CloseableHttpClient createSSLClientDefault() throws Exception { //如果下面的方法證書還是不過,報(bào)錯(cuò)的話試試下面第二種 /* SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy(){ //信任所有 public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException { return true; } }).build(); SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext); return HttpClients.custom().setSSLSocketFactory(sslsf).build();*/ SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(), NoopHostnameVerifier.INSTANCE); return HttpClients.custom().setSSLSocketFactory(sslsf).build(); } }
測試代碼
import com.alibaba.fastjson.JSONObject; import java.io.IOException; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * @author lrx * @description: TODO 小程序推送 * @date 2022/4/11 13:32 */ public class TestXCXMain { public static void main(String[] args) throws Exception { String appid = ""; //appid String secret = ""; //secret //登錄鏈接獲取token String loginUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret; Map<String, Object> payloadMap = JSONObject.parseObject(HttpClientUtils.httpGet(loginUrl)); String token = null; if (payloadMap.containsKey("access_token")) { token = payloadMap.get("access_token").toString(); } System.out.println("獲取token" + token); Map<String, Object> qqMap = new HashMap<String, Object>(); qqMap.put("touser", "openid"); //要推送的openid qqMap.put("template_id", ""); //信息模板id qqMap.put("page", "index"); qqMap.put("miniprogram_state", "developer"); qqMap.put("lang", "zh_CN"); //封裝信息 Map<String, Object> dataMap = new HashMap<String, Object>(); Map<String, String> valueMap1 = new HashMap<String, String>(); valueMap1.put("value", "成功"); Map<String, String> valueMap2 = new HashMap<String, String>(); valueMap2.put("value", "成功"); dataMap.put("thing3", valueMap1); dataMap.put("thing1", valueMap2); qqMap.put("data", dataMap); //發(fā)送 Map<String, Object> payloadMapData = JSONObject.parseObject(HttpClientUtils.httpPost("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="+token,JSONObject.toJSONString(qqMap))); if (payloadMapData.containsKey("errCode")) { System.out.println("返回code碼:"+payloadMapData.get("errCode").toString()); } } }
5.推送結(jié)果
到此這篇關(guān)于SpringBoot小程序推送信息的項(xiàng)目實(shí)踐的文章就介紹到這了,更多相關(guān)SpringBoot小程序推送信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java設(shè)計(jì)通用的返回?cái)?shù)據(jù)格式過程講解
現(xiàn)在很多的項(xiàng)目server端返回client端的數(shù)據(jù)多數(shù)以JSON格式返回,同時(shí)結(jié)合其它需要,通常加一下狀態(tài)碼和信息之類,給前端處理帶來很大的方便,這篇文章就用Java設(shè)計(jì)了通用的返回?cái)?shù)據(jù)格式,感興趣的同學(xué)可以參考下文2023-05-05elasticsearch元數(shù)據(jù)構(gòu)建metadata及routing類源碼分析
這篇文章主要為大家介紹了elasticsearch元數(shù)據(jù)構(gòu)建metadata?routing類內(nèi)部源碼分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04SpringBoot中操作Bean的生命周期的方法總結(jié)
在SpringBoot應(yīng)用中,管理和操作Bean的生命周期是一項(xiàng)關(guān)鍵的任務(wù),這不僅涉及到如何創(chuàng)建和銷毀Bean,還包括如何在應(yīng)用的生命周期中對Bean進(jìn)行精細(xì)控制,本文給大家總結(jié)了SpringBoot中操作Bean的生命周期的方法,需要的朋友可以參考下2023-12-12java Hibernate save()與persist()區(qū)別
本文章來給各位同學(xué)介紹一下Hibernate save()與persist()區(qū)別,希望此文章能對各位同學(xué)對于Hibernate save()與persist()有所理解2016-01-01多個(gè)sheet Excel 數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫的實(shí)現(xiàn)方法
這篇文章主要介紹了多個(gè)sheet Excel 數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03