SpringBoot微信消息接口配置詳解
1.申請測試號,并記錄appID和appsecret
2.關(guān)注測試號
3.添加消息模板
{{topic.DATA}} 用戶名: {{user.DATA}} 單車編號:{{car.DATA}} 鎖定時間:{{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ā)送消息的接口的訪問憑證 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")注解會報黃,需要導(dǎo)入依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency>
2.填寫配置文件application.yml
以下兩個值會被注入到WechatConf 中
wechat: app-id: wxc67a533f22dc2f9c appsecret: <your appsecret>
3.注入發(fā)送Http請求的對象
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.后臺調(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.使用定時任務(wù)獲取后臺訪問微信接口的憑證AccessToken
注意:需在啟動類上添加開啟定時任務(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過期時間為7200s,這里定時7100s執(zhí)行一次定時任務(wù) */ @Scheduled(initialDelay = 2000, fixedDelay = 7100 * 1000) public void refreshToken() { // 請求方式: GET // URL:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET // 發(fā)起一個get請求,返回的數(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ā)送消息接口的請求參數(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"); //點擊詳情跳轉(zhuǎn)的地址 messageTemplate.setUrl("http://www.baidu.com"); //設(shè)置模板dada參數(shù) messageTemplate.getData().put("topic", MessageTemplate.initData("您的單車已經(jī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ào)用微信接口,發(fā)送模板消息 Result result = restTemplate.postForObject(String.format(WechatConf.PUSH_MESSAGE_URL, wechatConf.getAccessToken()), messageTemplate, Result.class); return result; } }
測試
1.運行項目,發(fā)送請求
2.接收到微信提醒消息
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- springboot整合微信支付sdk過程解析
- springboot 微信授權(quán)網(wǎng)頁登錄操作流程
- SpringBoot 微信退款功能的示例代碼
- Springboot單體架構(gòu)http請求轉(zhuǎn)換https請求來支持微信小程序調(diào)用接口
- 基于springboot微信公眾號開發(fā)(微信自動回復(fù))
- SpringBoot JS-SDK自定義微信分享的實現(xiàn)
- 微信小程序 springboot后臺如何獲取用戶的openid
- SpringBoot中獲取微信用戶信息的方法
- Spring Boot獲取微信用戶信息的超簡單方法
- activemq整合springboot使用方法(個人微信小程序用)
- Springboot網(wǎng)站第三方登錄 微信登錄
- Spring Boot項目中集成微信支付v3
相關(guān)文章
@Transactional遇到try catch失效的問題
這篇文章主要介紹了@Transactional遇到try catch失效的問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01如何解決Maven打包時每次都出現(xiàn)Download maven-metadata.xml卡住問題
這篇文章主要介紹了如何解決Maven打包時每次都出現(xiàn)Download maven-metadata.xml卡住問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05logback的FileAppender文件追加模式和沖突檢測解讀
這篇文章主要為大家介紹了logback的FileAppender文件追加模式和沖突檢測解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10Springboot logback-spring.xml無法加載問題
這篇文章主要介紹了Springboot logback-spring.xml無法加載問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05如何用java程序(JSch)運行遠程linux主機上的shell腳本
這篇文章主要介紹了如何用java程序(JSch)運行遠程linux主機上的shell腳本,幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-08-08