1、引入微信开发工具包:
wixin-java-tools这个开源的SDK。
下载地址
2、引入maven地址:jersey 是基于Java的一个轻量级RESTful风格的Web Services框架。
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
</dependency>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>2.8.0</version>
</dependency>
3、网页授权:
文档地址
第一步:我们需要配置一个回调的网址url。
String url="http://localhost:8080/sell/wechat/userInfo";
第二步:调用sdk的方法
wxMpService.oauth2buildAuthorizationUrl(url, WxConsts.OAuth2Scope.SNSAPI_USERINFO, null)
第三步:重定向到http://localhost:8080/sell/wechat/userInfo
@GetMapping("/authorize")
public String authorize(@RequestParam("returnUrl") String returnUrl){
//1. 配置
//2.调用方法
String url="http://localhost:8080/sell/wechat/userInfo";
String redirectUrl=wxMpService.oauth2buildAuthorizationUrl(url,WxConsts.OAUTH2_SCOPE_BASE, URLEncoder.encode(returnUrl));
log.info("【微信网页授权】获取code,redirectUrl={}",redirectUrl);
return "redirect:"+redirectUrl;
}
注册服务接口:
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: weixin
微信消息模板接口
概述
模板消息仅用于公众号向用户发送重要的服务通知,只能用于符合其要求的服务场景中,如信用卡刷卡通知,商品购买成功通知等。不支持广告等营销类消息以及其它所有可能对用户造成骚扰的消息。
参考网址: https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1433751277
@RestController
@RequestMapping("/weiXin")
public class WeiXinCatController {
@Autowired
private WxMpService wxService;
@RequestMapping("/sendTemplate")
public String createWeiXinCat(@RequestBody WxMpTemplateMessage wxMpTemplateMessage) throws WxErrorException {
WxMpTemplateMsgService templateMsgService = wxService.getTemplateMsgService();
return templateMsgService.sendTemplateMsg(wxMpTemplateMessage);
}
}
参数
请求地址http://127.0.0.1:81/weiXin/sendTemplate
{
"toUser": "okYSmt0cItTc4dZXFn43BbmYW8Rw",
"templateId": "eLozPgRNCPMEgOWGHwNW6cXkfrVqtsyDsyEwVabu0ww",
"url": "http://www.xxx.com",
"data": [
{
"name": "first",
"value": "2017年11月06日 23:32",
"color": "#173177"
},
{
"name": "keyword1",
"value": "30",
"color": "#173177"
},
{
"name": "keyword2",
"value": "201410515111522",
"color": "#173177"
}
]
}