2018.06.06
springboot项目需要启动才能够与微信公众平台建立连接;并且需要公网的网址或者自己找一个内网穿透工具
内网穿透工具找natapp就行,直接百度natapp,根据自己的电脑系统下载客户端,详细方法在那下边都写了
微信公众号申请不是用的微信号
微信javatools
文档:https://github.com/chanjarster/weixin-java-tools/wiki
更详细的操作看文档吧,我也没做,看文档的时候注意看版本号,版本不一样,具体的实现细节也是不一样的
pom依赖是这个
com.github.binarywang
weixin-java-mp
3.0.6.BETA
com.github.binarywang
weixin-java-common
3.0.6.BETA
我用着还行,我只做了简单的与微信公众平台链接,然后互动
先启动springboot项目后,使用下面这个方法与微信公众平台建立连接,
注意先启动项目,微信公众平台上才能提交配置成功
package com.java1234.weixin.servlet;
import com.java1234.weixin.util.ServiceImpl;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(name = "ServletCon")
@Controller
public class ServletCon extends HttpServlet {
@RequestMapping(value="/bb",method=RequestMethod.POST)
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
WxMpXmlMessage message=WxMpXmlMessage.fromXml(request.getInputStream());
ServiceImpl serv = new ServiceImpl();
serv.serv(message,response);
}
@RequestMapping(value="/bb",method=RequestMethod.GET)
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//微信服务器get传递的参数
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
//微信工具类
WxMpService wxService=new WxMpServiceImpl();
//注入token的配置参数
/**
* 生产环境 建议将WxMpInMemoryConfigStorage持久化
*/
WxMpInMemoryConfigStorage wxConfigProvider=new WxMpInMemoryConfigStorage();
//注入token值
wxConfigProvider.setToken("weixin");
wxService.setWxMpConfigStorage(wxConfigProvider);
boolean flag=wxService.checkSignature(timestamp, nonce, signature);
PrintWriter out=response.getWriter();
if(flag){
out.print(echostr);
}
out.close();
out=null;
}
}
下面这个方法是接收公众号消息并给公众号返回消息的
/**
* Copyright (C), 2015-2018, XXX有限公司
* FileName: ServiceImpl
* Author: 13645
* Date: 2018/5/17 17:24
* Description:
* History:
*
下面这个是上面那个方法掉用的具体方法
package com.java1234.weixin.util.utilmpl;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import java.io.File;
public class MessageCreate {
//返回微信文本消息
public void createText(WxMpService wxService,String fromUser,String cont) throws WxErrorException {
System.out.println("创建文本消息");
WxMpKefuMessage message=WxMpKefuMessage.TEXT().toUser(fromUser).content(cont).build();
wxService.getKefuService().sendKefuMessage(message);
}
//返回微信图片消息
public void createPicture(WxMpService wxService,String fromUser,File file) throws WxErrorException {
System.out.println("创建图片消息");
WxMediaUploadResult result=null;
result=wxService.getMaterialService().mediaUpload(WxConsts.MediaFileType.IMAGE,file);
if(result !=null){
String media_Id=result.getMediaId();
WxMpKefuMessage message=WxMpKefuMessage.IMAGE().mediaId(media_Id).toUser(fromUser).build();
wxService.getKefuService().sendKefuMessage(message);
}
}
//返回微信语音消息
public void createVoice(WxMpService wxService,String fromUser,File file) throws WxErrorException {
System.out.println("创建语音消息");
WxMediaUploadResult result=null;
result=wxService.getMaterialService().mediaUpload(WxConsts.MediaFileType.VOICE,file);
if(result !=null){
String media_Id=result.getMediaId();
WxMpKefuMessage message=WxMpKefuMessage.VOICE().mediaId(media_Id).toUser(fromUser).build();
wxService.getKefuService().sendKefuMessage(message);
}
}
//返回微信视频消息
public void createVideo(WxMpService wxService,String fromUser,File file1,File file2) throws WxErrorException {
System.out.println("创建视频消息");
WxMediaUploadResult result1=null;
WxMediaUploadResult result2=null;
result1=wxService.getMaterialService().mediaUpload(WxConsts.MediaFileType.VIDEO,file1);
result2=wxService.getMaterialService().mediaUpload(WxConsts.MediaFileType.IMAGE,file2);
if(result1 !=null){
String media_Id = result1.getMediaId();
String thumMedia_Id=result2.getMediaId();
System.out.println("缩略图id:"+thumMedia_Id);
WxMpKefuMessage message=WxMpKefuMessage.VIDEO().toUser(fromUser).title("testM.mp4").mediaId(media_Id).thumbMediaId(thumMedia_Id).description("testMM").build();
wxService.getKefuService().sendKefuMessage(message);
}
}
//返回微信图文消息
public void createTUWEN(WxMpService wxService,String fromUser) throws WxErrorException {
System.out.println("创建图文消息");
WxMpKefuMessage.WxArticle article1=new WxMpKefuMessage.WxArticle();
article1.setUrl("http://www.baidu.com");
article1.setPicUrl("http://yushangai.s1.natapp.cc/media/2.jpg");
article1.setDescription("Is Really A Happy Day");
article1.setTitle("开心吗");
WxMpKefuMessage.WxArticle article2=new WxMpKefuMessage.WxArticle();
article2.setUrl("http://www.yushangai.top");
article2.setPicUrl("http://yushangai.s1.natapp.cc/media/dog.jpg");
article2.setDescription("看它的小眼神");
article2.setTitle("抽烟的狗");
WxMpKefuMessage.WxArticle article3=new WxMpKefuMessage.WxArticle();
article3.setUrl("http://www.yushangai.top");
article3.setPicUrl("http://yushangai.s1.natapp.cc/media/2.jpg");
article3.setDescription("看它的小胡子");
article3.setTitle("弹吉他");
WxMpKefuMessage message = WxMpKefuMessage.NEWS().addArticle(article1).addArticle(article2).addArticle(article3).toUser(fromUser).build();
wxService.getKefuService().sendKefuMessage(message);
}
}
这些就是具体的方法了,在这里感谢一下Code猿猿,最初是从他那看的,不过他的那个版本在maven上找不到了
代码并不重要,网上能找到一堆,重要的是你要大概的理解微信公众号的接入流程,剩下的代码看文档也能自己慢慢敲出来
微信公众平台配置提交成功了你就成功了一半了