SpringBoot微信消息接口配置詳解
1.申請(qǐng)測(cè)試號(hào),并記錄appID和appsecret
2.關(guān)注測(cè)試號(hào)
3.添加消息模板
{{topic.DATA}} 用戶名: {{user.DATA}} 單車編號(hào):{{car.DATA}} 鎖定時(shí)間:{{date.DATA}} {{remark.DATA}}
微信接口配置和代碼
1.添加微信配置文件
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Configuration; @Configuration @ConfigurationProperties(prefix = "wechat") public class WechatConf { // 獲取accessToken的接口 public static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s"; // 發(fā)送消息的接口 public static final String PUSH_MESSAGE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s"; private String appId; private String appsecret; // 發(fā)送消息的接口的訪問(wèn)憑證 private String accessToken; public String getAppId() { return appId; } public void setAppId(String appId) { this.appId = appId; } public String getAppsecret() { return appsecret; } public void setAppsecret(String appsecret) { this.appsecret = appsecret; } public String getAccessToken() { return accessToken; } public void setAccessToken(String accessToken) { this.accessToken = accessToken; } }
這里@ConfigurationProperties(prefix = "wechat")注解會(huì)報(bào)黃,需要導(dǎo)入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
2.填寫(xiě)配置文件application.yml
以下兩個(gè)值會(huì)被注入到WechatConf 中
wechat: app-id: wxc67a533f22dc2f9c appsecret: <your appsecret>
3.注入發(fā)送Http請(qǐng)求的對(duì)象
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @Configuration public class RestConf { @Bean public RestTemplate getRestTemplate() { return new RestTemplate(); } }
4.后臺(tái)調(diào)用微信接口憑證AccessToken的封裝類
import com.fasterxml.jackson.annotation.JsonProperty; public class AccessToken { @JsonProperty("access_token") private String accessToken; @JsonProperty("expires_in") private Long expiresIn; public String getAccessToken() { return accessToken; } public void setAccessToken(String accessToken) { this.accessToken = accessToken; } public Long getExpiresIn() { return expiresIn; } public void setExpiresIn(Long expiresIn) { this.expiresIn = expiresIn; } }
5.使用定時(shí)任務(wù)獲取后臺(tái)訪問(wèn)微信接口的憑證AccessToken
注意:需在啟動(dòng)類上添加開(kāi)啟定時(shí)任務(wù)的注解@EnableScheduling
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import com.thy.common.AccessToken; import com.thy.config.WechatConf; @Component public class MessageTask { @Autowired private RestTemplate restTemplate; @Autowired private WechatConf wechatConf; /** * initialDelay: 初始化2s后執(zhí)行第一次 * fixedDelay:微信默認(rèn)Token過(guò)期時(shí)間為7200s,這里定時(shí)7100s執(zhí)行一次定時(shí)任務(wù) */ @Scheduled(initialDelay = 2000, fixedDelay = 7100 * 1000) public void refreshToken() { // 請(qǐng)求方式: GET // URL:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET // 發(fā)起一個(gè)get請(qǐng)求,返回的數(shù)據(jù)json文本,使用json工具將json文本直接轉(zhuǎn)化為Class<?> AccessToken accessToken = restTemplate.getForObject( String.format(WechatConf.GET_TOKEN_URL, wechatConf.getAppId(), wechatConf.getAppsecret()), AccessToken.class); // 將獲取的accessToken注入wechatConf wechatConf.setAccessToken(accessToken.getAccessToken()); } }
6.發(fā)送消息接口的請(qǐng)求參數(shù)的封裝類
import java.util.HashMap; import java.util.Map; import com.fasterxml.jackson.annotation.JsonProperty; public class MessageTemplate { @JsonProperty("touser") private String toUser; @JsonProperty("template_id") private String templateId; private String url; private Map<String,String> miniprogram; private Map<String, Map<String, String>> data = new HashMap<>(); public static Map<String, String> initData(String value, String color) { HashMap<String, String> data = new HashMap<String, String>(); data.put("value", value); data.put("color", color); return data; } public String getToUser() { return toUser; } public void setToUser(String toUser) { this.toUser = toUser; } public String getTemplateId() { return templateId; } public void setTemplateId(String templateId) { this.templateId = templateId; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Map<String, String> getMiniprogram() { return miniprogram; } public void setMiniprogram(Map<String, String> miniprogram) { this.miniprogram = miniprogram; } public Map<String, Map<String, String>> getData() { return data; } public void setData(Map<String, Map<String, String>> data) { this.data = data; } }
7.發(fā)送消息接口的返回值的封裝類
public class Result { private Integer errcode; private String errmsg; private Long msgid; public Integer getErrcode() { return errcode; } public void setErrcode(Integer errcode) { this.errcode = errcode; } public String getErrmsg() { return errmsg; } public void setErrmsg(String errmsg) { this.errmsg = errmsg; } public Long getMsgid() { return msgid; } public void setMsgid(Long msgid) { this.msgid = msgid; } }
8.消息發(fā)送接口
import java.text.SimpleDateFormat; import java.util.Date; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import com.thy.common.MessageTemplate; import com.thy.common.Result; import com.thy.config.WechatConf; import com.thy.pojo.UserCarInfo; @RestController public class MessageController { @Autowired private RestTemplate restTemplate; @Autowired private WechatConf wechatConf; @RequestMapping("/sendMessage") public Object sendMessage(@RequestBody UserCarInfo userCarInfo) { MessageTemplate messageTemplate = new MessageTemplate(); // 設(shè)置模板id messageTemplate.setTemplateId("othsG1ZD5w9ywTGyV6XCECnY1Q1oAIY5e-NvF94fzAI"); // 設(shè)置接收用戶openId messageTemplate.setToUser("oUbk_1bVlUGqhMzQHMC_jbkysMgY"); //點(diǎn)擊詳情跳轉(zhuǎn)的地址 messageTemplate.setUrl("http://www.baidu.com"); //設(shè)置模板dada參數(shù) messageTemplate.getData().put("topic", MessageTemplate.initData("您的單車已經(jīng)鎖定成功,騎行請(qǐng)注意安全!\n", "")); messageTemplate.getData().put("user", MessageTemplate.initData(userCarInfo.getUserName(), "#0000EE")); messageTemplate.getData().put("car", MessageTemplate.initData(userCarInfo.getCarSn(), "#00CD00")); SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); messageTemplate.getData().put("date", MessageTemplate.initData(simpleDateFormat.format(new Date())+"\n", "")); messageTemplate.getData().put("remark", MessageTemplate.initData("點(diǎn)擊詳情可查看您的租車信息", "")); //調(diào)用微信接口,發(fā)送模板消息 Result result = restTemplate.postForObject(String.format(WechatConf.PUSH_MESSAGE_URL, wechatConf.getAccessToken()), messageTemplate, Result.class); return result; } }
測(cè)試
1.運(yùn)行項(xiàng)目,發(fā)送請(qǐng)求
2.接收到微信提醒消息
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot整合微信支付sdk過(guò)程解析
- springboot 微信授權(quán)網(wǎng)頁(yè)登錄操作流程
- SpringBoot 微信退款功能的示例代碼
- Springboot單體架構(gòu)http請(qǐng)求轉(zhuǎn)換https請(qǐng)求來(lái)支持微信小程序調(diào)用接口
- 基于springboot微信公眾號(hào)開(kāi)發(fā)(微信自動(dòng)回復(fù))
- SpringBoot JS-SDK自定義微信分享的實(shí)現(xiàn)
- 微信小程序 springboot后臺(tái)如何獲取用戶的openid
- SpringBoot中獲取微信用戶信息的方法
- Spring Boot獲取微信用戶信息的超簡(jiǎn)單方法
- activemq整合springboot使用方法(個(gè)人微信小程序用)
- Springboot網(wǎng)站第三方登錄 微信登錄
- Spring Boot項(xiàng)目中集成微信支付v3
相關(guān)文章
@Transactional遇到try catch失效的問(wèn)題
這篇文章主要介紹了@Transactional遇到try catch失效的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-01-01RestTemplate自定義請(qǐng)求失敗異常處理示例解析
這篇文章主要為大家介紹了RestTemplate自定義請(qǐng)求失敗異常處理的示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-03-03如何解決Maven打包時(shí)每次都出現(xiàn)Download maven-metadata.xml卡住問(wèn)題
這篇文章主要介紹了如何解決Maven打包時(shí)每次都出現(xiàn)Download maven-metadata.xml卡住問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05logback的FileAppender文件追加模式和沖突檢測(cè)解讀
這篇文章主要為大家介紹了logback的FileAppender文件追加模式和沖突檢測(cè)解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-10-10Springboot logback-spring.xml無(wú)法加載問(wèn)題
這篇文章主要介紹了Springboot logback-spring.xml無(wú)法加載問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05RabbitMq消息防丟失功能實(shí)現(xiàn)方式講解
這篇文章主要介紹了RabbitMq消息防丟失功能實(shí)現(xiàn),RabbitMQ中,消息丟失可以簡(jiǎn)單的分為兩種:客戶端丟失和服務(wù)端丟失。針對(duì)這兩種消息丟失,RabbitMQ都給出了相應(yīng)的解決方案2023-01-01如何用java程序(JSch)運(yùn)行遠(yuǎn)程linux主機(jī)上的shell腳本
這篇文章主要介紹了如何用java程序(JSch)運(yùn)行遠(yuǎn)程linux主機(jī)上的shell腳本,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-08-08