SpringBoot小程序推送信息的項(xiàng)目實(shí)踐
1.小程序推送信息列如我們?nèi)ゲ蛷d等位有預(yù)約提醒,剩余桌數(shù)
首先申請一個(gè)小程序,微信開放平臺:小程序
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)文章
SpringSecurity定義多個(gè)過濾器鏈的操作代碼
Spring?Security?是?Spring家族中的一個(gè)安全管理框架。相比與另外一個(gè)安全框架Shiro,它提供了更豐富的功能,社區(qū)資源也比Shiro豐富,今天通過本文給大家介紹SpringSecurity定義多個(gè)過濾器鏈的實(shí)例,感興趣的朋友跟隨小編一起看看吧2023-04-04
你必須得會的SpringBoot全局統(tǒng)一處理異常詳解
程序在運(yùn)行的過程中,不可避免會產(chǎn)生各種各樣的錯(cuò)誤,這個(gè)時(shí)候就需要進(jìn)行異常處理,本文主要為大家介紹了SpringBoot實(shí)現(xiàn)全局統(tǒng)一處理異常的方法,需要的可以參考一下2023-06-06
SpringBoot+Shiro+LayUI權(quán)限管理系統(tǒng)項(xiàng)目源碼
本項(xiàng)目旨在打造一個(gè)基于RBAC架構(gòu)模式的通用的、并不復(fù)雜但易用的權(quán)限管理系統(tǒng),通過SpringBoot+Shiro+LayUI權(quán)限管理系統(tǒng)項(xiàng)目可以更好的幫助我們掌握springboot知識點(diǎn),感興趣的朋友一起看看吧2021-04-04
mybatis 自定義實(shí)現(xiàn)攔截器插件Interceptor示例
這篇文章主要介紹了mybatis 自定義實(shí)現(xiàn)攔截器插件Interceptor,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10
java轉(zhuǎn)樹形結(jié)構(gòu)工具類詳解
這篇文章主要為大家詳細(xì)介紹了java轉(zhuǎn)樹形結(jié)構(gòu)工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
springboot掃碼登錄的簡單實(shí)現(xiàn)
本文主要介紹基于SpringBoot + Vue + Android實(shí)現(xiàn)的掃碼登錄,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
Dapr在Java中的服務(wù)調(diào)用實(shí)戰(zhàn)過程詳解
這篇文章主要為大家介紹了Dapr在Java中的服務(wù)調(diào)用實(shí)戰(zhàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Java之BigDecimal實(shí)現(xiàn)詳解
這篇文章主要介紹了Java之BigDecimal實(shí)現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
通過端口1433連接到主機(jī)127.0.0.1的 TCP/IP 連接失敗,錯(cuò)誤:“connect timed out”的解
這篇文章主要介紹了通過端口1433連接到主機(jī)127.0.0.1的 TCP/IP 連接失敗,錯(cuò)誤:“connect timed out”的解決方法,需要的朋友可以參考下2015-08-08

