本文实例为大家分享了java微信公众号发送消息模板的具体代码,供大家参考,具体内容如下

这段时间接触公众号开发,写下向用户发送消息模板的接口调用
先上接口代码
public static JSONObject sendModelMessage(ServletContext context,JSONObject jsonMsg) {
System.out.println("消息内容:"+jsonMsg);
boolean result = false;
try {
getWX_AccessToken(context);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 拼接请求地址
String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
requestUrl = requestUrl.replace("ACCESS_TOKEN", context.getAttribute(ContextTokenName).toString());
// 发送客服消息
JSONObject jsonObject = getJsonByWX(requestUrl, context, "POST",jsonMsg, false);
if (null != jsonObject) {
int errorCode = jsonObject.getInt("errcode");
String errorMsg = jsonObject.getString("errmsg");
if (0 == errorCode) {
result = true;
System.out.println("模板消息发送成功 errcode:{} "+errorCode+"----"+errorMsg);
} else {
System.out.println("模板消息发送失败 errcode:{} "+errorCode+"----"+errorMsg);
}
}
return null;
} 
售后响应及时
7×24小时客服热线
数据备份
更安全、更高效、更稳定
价格公道精准
项目经理精准报价不弄虚作假
合作无风险
重合同讲信誉,无效全额退款