2018.06.06
springboot项目需要启动才能够与微信公众平台建立连接;并且需要公网的网址或者自己找一个内网穿透工具
内网穿透工具找natapp就行,直接百度natapp,根据自己的电脑系统下载客户端,详细方法在那下边都写了
微信公众号申请不是用的微信号
微信javatools
文档:https://github.com/chanjarster/weixin-java-tools/wiki
更详细的操作看文档吧,我也没做,看文档的时候注意看版本号,版本不一样,具体的实现细节也是不一样的
pom依赖是这个
<!-- https://mvnrepository.com/artifact/com.github.binarywang/weixin-java-mp -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.0.6.BETA</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.binarywang/weixin-java-common -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-common</artifactId>
<version>3.0.6.BETA</version>
</dependency>
我用着还行,我只做了简单的与微信公众平台链接,然后互动
先启动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:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
package com.java1234.weixin.util;
import com.java1234.weixin.util.utilmpl.MessageCreate;
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.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage;
import me.chanjar.weixin.mp.bean.result.WxMpMassUploadResult;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
/**
* 〈一句话功能简述〉<br>
* 〈〉
*
* @author 13645
* @create 2018/5/17
* @since 1.0.0
*/
public class ServiceImpl {
public void serv(WxMpXmlMessage message, HttpServletResponse response){
//配置微信参数 获取wxService
WxMpService wxService=new WxMpServiceImpl();
response.setCharacterEncoding("utf-8");
//微信配置参数
WxMpInMemoryConfigStorage wxConfigProvider=new WxMpInMemoryConfigStorage();
//花儿与昕,这是我的公众号,测试用的
wxConfigProvider.setAppId("appid");
wxConfigProvider.setSecret("appsecret");
//
wxService.setWxMpConfigStorage(wxConfigProvider);
//消息处理
String messageType=message.getMsgType(); //消息类型
String fromUser=message.getFromUser();
String toUser=message.getToUser();
String content=message.getContent();
System.out.println(fromUser);
MessageCreate messageCreate = new MessageCreate();
//输入返回消息、
if(content.equals("文本")) {
System.out.println("文本消息========");
String cont="我是文本";
try {
messageCreate.createText(wxService,fromUser,cont);
} catch (WxErrorException e) {
e.printStackTrace();
}
}
if(content.equals("图片")) {
System.out.println("图片消息========");
File file=new File("D:"+File.separator+"someWJ"+File.separator+"抽烟的狗.jpg");
try {
messageCreate.createPicture(wxService,fromUser,file);
} catch (WxErrorException e) {
e.printStackTrace();
}
}
if (content.equals("语音")){
System.out.println("语音消息========");
File file=new File("D:"+File.separator+"someWJ"+File.separator+"music.mp3");
try {
messageCreate.createVoice(wxService,fromUser,file);
} catch (WxErrorException e) {
e.printStackTrace();
}
}
if (content.equals("图文")){
System.out.println("图文消息========");
try {
messageCreate.createTUWEN(wxService,fromUser);
} catch (WxErrorException e) {
e.printStackTrace();
}
}
if(content.equals("视频")){
System.out.println("视频消息========");
File file1=new File("D:"+File.separator+"someWJ"+File.separator+"testM.mp4");
File file2=new File("D:"+File.separator+"someWJ"+File.separator+"抽烟的狗.jpg");
try {
messageCreate.createVideo(wxService,fromUser,file1,file2);
} catch (WxErrorException e) {
e.printStackTrace();
}
}
}
}
下面这个是上面那个方法掉用的具体方法
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上找不到了
代码并不重要,网上能找到一堆,重要的是你要大概的理解微信公众号的接入流程,剩下的代码看文档也能自己慢慢敲出来
微信公众平台配置提交成功了你就成功了一半了