In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the SpringBoot Mini Program push message how to achieve the relevant knowledge, the content is detailed and easy to understand, easy to operate, with a certain reference value, I believe that after reading this SpringBoot Mini Program push message how to achieve the article will have a harvest, let's take a look at it.
1. Mini Program push messages are listed, such as we go to the restaurant and other places have an appointment to remind us, the number of tables left
First, apply for a Mini Program, Wechat open platform: Mini Program
two。 Apply for Mini Program information, apply for information template
Appid
AppSecret
3. Develop according to the development documentation
SubscribeMessage.send | Wechat Open documents
4. The code is as follows:
Introduce dependency
Org.apache.httpcomponents httpclient 4.5.2
First prepare a HTTP utility class 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 9:50 on 2021-3-9 * / public class HttpClientUtils {/ / Http protocol GET request public static String httpGet (String url) throws IOException {/ / initialize HttpClient CloseableHttpClient httpClient = HttpClients.createDefault (); / / create HttpGet HttpGet httpGet = new HttpGet (url) / / initiate a request and get the response object CloseableHttpResponse response = httpClient.execute (httpGet); / / get the request status code / / response.getStatusLine () .getStatusCode (); / / get the returned data entity object HttpEntity entity = response.getEntity (); / / convert to the string String result = EntityUtils.toString (entity, "UTF-8"); return result } / / Http protocol Post requests public static String httpPost (String url, String json) throws Exception {/ / initial HttpClient CloseableHttpClient httpClient = HttpClients.createDefault (); / / create Post object HttpPost httpPost = new HttpPost (url); / / set 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); / / initiate a request to get the response object CloseableHttpResponse response = httpClient.execute (httpPost); / / get the request code / / response.getStatusLine () .getStatusCode () / / get the returned data entity object HttpEntity entity = response.getEntity (); / / convert to the string String result = EntityUtils.toString (entity, "UTF-8"); return result;} / / Https protocol Get request 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 Protocol Post request 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 {/ / if the following method certificate still fails, try the following second method / * SSLContext sslContext = new SSLContextBuilder () .loadTrustMaterial (null, new TrustStrategy () {/ / trust all 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 ();}}
Test code
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 Mini Program push * @ date 13:32 on 2022-4-11 * / public class TestXCXMain {public static void main (String [] args) throws Exception {String appid = "; / / appid String secret =" / / secret / / login link to get token String loginUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid +" & secret= "+ secret; Map payloadMap = JSONObject.parseObject (HttpClientUtils.httpGet (loginUrl)); String token = null; if (payloadMap.containsKey (" access_token ")) {token = payloadMap.get (" access_token ") .toString () } System.out.println ("get token" + token); Map qqMap = new HashMap (); qqMap.put ("touser", "openid"); / / openid qqMap.put to be pushed ("template_id", ""); / / Information template id qqMap.put ("page", "index"); qqMap.put ("miniprogram_state", "developer") QqMap.put ("lang", "zh_CN"); / / encapsulation information Map dataMap = new HashMap (); Map valueMap1 = new HashMap (); valueMap1.put ("value", "success"); Map valueMap2 = new HashMap (); valueMap2.put ("value", "success"); dataMap.put ("thing3", valueMap1) DataMap.put ("thing1", valueMap2); qqMap.put ("data", dataMap); / / send Map 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 ("return code:" + payloadMapData.get ("errCode"). ToString ());}} 5. Push result
This is the end of the article on "how to implement the SpringBoot Mini Program push message". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to achieve SpringBoot Mini Program push information". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.