千家信息网

Java怎么实现微信公众号发送模版消息

发表于:2026-11-04 作者:千家信息网编辑
千家信息网最后更新 2026年11月04日,这篇文章主要介绍Java怎么实现微信公众号发送模版消息,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!微信公众号发送模版消息 背景:如下图,当用户发布需求的时候,公众号自定推送消
千家信息网最后更新 2026年11月04日Java怎么实现微信公众号发送模版消息

这篇文章主要介绍Java怎么实现微信公众号发送模版消息,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

微信公众号发送模版消息 背景:如下图,当用户发布需求的时候,公众号自定推送消息。例如:微信支付的时候,公众号会推送支付成功消息

前提:发送模版消息,顾名思义,前提就是需要有模版,那么在哪里配置模版呢?
微信公众号平台–>广告与服务–>模版消息–>我的模版
模版消息是已经申请过的模版,如果里面的模版都不符合自己业务的话,可以到模版库里找,然后添加到「我的模版」。也可以按照自己的需求申请新的模版,一般第二个工作日会审核通过。

在模版详情可以查看模版的格式,下图左边红框是消息最终展示的效果,
右边红框是需要传的参数。

有了模版之后,模版ID就是我们要放进代码里的,复制出来。

消息模版准备好之后,暂时不要写代码奥,查看微信开发文档,看看发送模版都需要哪些参数。
微信开发文档–>基础消息能力–>模版消息接口–「发送模版消息」

查看微信开发文档

发送模版消息
http请求方式: POST https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN

注:url和miniprogram都是非必填字段,若都不传则模板无跳转;若都传,会优先跳转至小程序。开发者可根据实际需要选择其中一种跳转方式即可。当用户的微信客户端版本不支持跳小程序时,将会跳转至url。
返回码说明
在调用模板消息接口后,会返回JSON数据包。
正常时的返回JSON数据包示例:
{
"errcode":0,
"errmsg":"ok",
"msgid":200228332
}

发送模版所需参数:
模版ID和openId是必须有的,剩下的就是和自己业务有关了。

上面的内容都搞定之后,就可以开始撸代码了

发送模版微信返回Dto

@Datapublic class TemplateMsgResultDto extends ResultState {    /**     * 消息id(发送模板消息)     */    private String msgid;}

发送模版微信返回状态

@Datapublic class ResultState implements Serializable {    /**     * 状态     */    private int errcode;    /**     * 信息     */    private String errmsg;}

微信模版消息请求参数实体类

@Datapublic class WxTemplateMsg {    /**     * 接收者openId     */    private String touser;    /**     * 模板ID     */    private String template_id;    /**     * 模板跳转链接     */    private String url;    // "miniprogram":{ 未加入    // "appid":"xiaochengxuappid12345",    // "pagepath":"index?foo=bar"    // },    /**     * data数据     */    private TreeMap> data;    /**     * 参数     *     * @param value 值     * @param color 颜色 可不填     * @return params     */    public static TreeMap item(String value, String color) {        TreeMap params = new TreeMap();        params.put("value", value);        params.put("color", color);        return params;    }}

Java封装模版信息代码

 public TemplateMsgResultDto noticeTemplate(TemplateMsgVo templateMsgVo) {        // 模版ID        String templateId="XXX";        TreeMap> params = new TreeMap<>();        //根据具体模板参数组装        params.put("first", WxTemplateMsg.item("恭喜!您的需求已发布成功", "#000000"));        params.put("keyword1", WxTemplateMsg.item(templateMsgVo.getTaskName(), "#000000"));        params.put("keyword2", WxTemplateMsg.item("需求已发布", "#000000"));        params.put("remark", WxTemplateMsg.item("请耐心等待审核", "#000000"));        WxTemplateMsg wxTemplateMsg = new WxTemplateMsg();        // 模版ID        wxTemplateMsg.setTemplate_id(templateId);        // openId        wxTemplateMsg.setTouser(templateMsgVo.getOpenId());        // 关键字赋值        wxTemplateMsg.setData(params);        String data = JsonUtils.ObjectToString(wxTemplateMsg);        return handleSendMsgLog(data);    }

发送模版代码

    private TemplateMsgResultDto handleSendMsgLog(String data) {        TemplateMsgResultDto resultDto = new TemplateMsgResultDto();        try {            resultDto = sendTemplateMsg(data);        } catch (Exception exception) {            log.error("发送模版失败",  exception);        }        // TODO 可以记录一下发送记录的日志        return resultDto;    } public TemplateMsgResultDto sendTemplateMsg(String data) throws Exception {        // 获取token        String accessToken = getAccessToken();        // 发送消息        HttpResult httpResult = HttpUtils.stringPostJson(ConstantsPath.SEND_MESSAGE_TEMPLATE_URL + accessToken, data);        return IMJsonUtils.getObject(httpResult.getBody(), TemplateMsgResultDto.class);    } /**     * 获取全局token     */    public String getAccessToken() {        String key = ConstantsRedisKey.ADV_WX_ACCESS_TOKEN;        // 从redis缓存中获取token        if (redisCacheManager.get(key) != null) {            return (String) redisCacheManager.get(key);        }        // 获取access_token        String url = String.format(ConstantsPath.WX_ACCESS_TOKEN_URL, appid, secret);        ResponseEntity result = restTemplate.getForEntity(url, String.class);        if (result.getStatusCode() == HttpStatus.OK) {            JSONObject jsonObject = JSON.parseObject(result.getBody());            String accessToken = jsonObject.getString("access_token");            // Long expires_in = jsonObject.getLong("expires_in");            redisCacheManager.set(key, accessToken, 1800);            return accessToken;        }        return null;    }

微信地址常量类

public class ConstantsPath {    /**     * 微信公众号获取全局token     */    public static final String WX_ACCESS_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s";     /**     * 微信发送模版消息     */    public static final String SEND_MESSAGE_TEMPLATE_URL = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=";}

Json工具类

@Slf4jpublic class JsonUtils {    private static ObjectMapper json;    static {        json = new ObjectMapper();        json.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));        json.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);    }    /**     * 序列化为JSON字符串     */    public static String ObjectToString(Object object) {        try {            return (json.writeValueAsString(object));        } catch (Exception e) {            log.error("序列化为JSON字符串出错",e);        }        return null;    }    public static  T getObject(String jsonString, Class clazz) {        if (StringUtils.isEmpty(jsonString))            return null;        try {            return json.readValue(jsonString, clazz);        } catch (Exception e) {            log.error("将JSON字符串转化为Map出错",e);            return null;        }    }}

Http工具类

@Component@Slf4jpublic class HttpUtils {    private static String sourcePath;    public static HttpResult stringPostJson(String path, String content) throws Exception{        return stringPost(path, null, content, "utf-8", "utf-8", "application/json");    }    public static HttpResult stringPost(String path, Map headerMap, String content, String contentencode, String encode, String contentType) throws Exception{        StringEntity entity = new StringEntity(content, contentencode);        entity.setContentType(contentType);        return post(path, headerMap, entity, encode);    }    private static HttpResult post(String path, Map headerMap, HttpEntity entity, String encode){        HttpResult httpResult = new HttpResult();        CloseableHttpClient httpClient = null;        CloseableHttpResponse response = null;        try{            HttpPost httpPost = new HttpPost(getURI(path));            LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();            httpClient = HttpClientBuilder.create().setRedirectStrategy(redirectStrategy).build();            RequestConfig requestConfig = RequestConfig.custom()                    .setSocketTimeout(120000)                    .setConnectTimeout(120000)                    .setConnectionRequestTimeout(120000)                    .setCircularRedirectsAllowed(true)                    .setRedirectsEnabled(true)                    .setMaxRedirects(5)                    .build();            httpPost.setConfig(requestConfig);            httpPost.setHeader("User-Agent", header);            if(headerMap != null && headerMap.size() > 0){                for(String name:headerMap.keySet()) {                    httpPost.addHeader(name, headerMap.get(name));                }            }            httpPost.setEntity(entity);            response = httpClient.execute(httpPost);            httpResult.setStatus(response.getStatusLine().getStatusCode());            if(httpResult.getStatus() == 200){                HttpEntity resEntity = response.getEntity();                httpResult.setBody(EntityUtils.toString(resEntity, encode));            }        }catch(Exception ex){            log.error("post请求出错", ex);        }finally{            try{                if(response != null){                    response.close();                }                if(httpClient != null){                    httpClient.close();                }            }catch(Exception ex) {                log.error("post请求关闭资源出错", ex);            }        }        return httpResult;    }}

以上是"Java怎么实现微信公众号发送模版消息"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!

模版 消息 公众 参数 模板 代码 需求 开发 内容 字符 字符串 就是 数据 文档 成功 业务 信息 全局 前提 工具 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技企业 广东万易互联网科技有限公司惠州分公司 亿人帮互联网科技有限公司 科技互联网公司装修 科技制造互联网公司 青岛互联网科技公司 猪兼强互联网科技 亿账通互联网科技 互联网养老 桔豆科技 联恩商钥互联网科技有限公司 新华互联网科技校企合作 互联网企业和科技型企业 互联网公司科技股 阿大互联网科技有限公司 允成互联网科技有限公司融资 上海科技产业互联网平台 陕西云淼互联网科技公司 全球科技与互联网公司市值变化 广西科技产业互联网是什么 银川新华互联网科技学校是国营吗 山东特亿宝互联网科技有限公司 互联网科技战争 狮子座互联网科技有限公司 苏州互联网科技贷款公司 滨湖区互联网智能科技产品供应 重庆互联网医院沙龙医盟信息科技 黑科技闪耀互联网之光 爱医互联网络科技 中天科技中国工业互联网 互联网科技企业商标注意事项
0