0.申請一個微信公眾號,記住他的appId,secret,token,accesstoken
1.創建一個springboot項目。在pom文件里面導入微信開發工具類
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.1.0</version>
</dependency>
2.編寫controller
 
          
         package com.weixin.demo.demo.web.back.controller; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutImageMessage; import me.chanjar.weixin.mp.bean.message.WxMpXmlOutTextMessage; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; @Controller public class weixinController { @Autowired private WxMpService wxMpService; /** * 1.接入公眾號:公眾號服務器配置的接口地址為這個 * @param signature * @param timestamp * @param nonce * @param echostr * @return */ @RequestMapping(value= "/wechart/index",method = RequestMethod.GET) @ResponseBody public String checkSignature(String signature, String timestamp, String nonce, String echostr) { System.out.println("————————微信接入——————————"); if (wxMpService.checkSignature(timestamp,nonce,signature)) { return echostr; }else { return null; } } /** * 消息的接收和回復 */ @PostMapping("/wechart/index") @ResponseBody public void sendWxMessage(HttpServletRequest request, HttpServletResponse response) throws IOException { System.out.println("————————微信消息接收和發送——————————"); //獲取消息流 WxMpXmlMessage message=WxMpXmlMessage.fromXml(request.getInputStream()); //消息的處理:文本 text String msgType = message.getMsgType();//消息的類型 String fromUser = message.getFromUser();//發送消息用戶的賬號 String toUser = message.getToUser();//開發者賬號 String content = message.getContent();//文本內容 String msg = message.getMsg(); //回復文本消息 if("text".equals(msgType)) { //創建文本消息內容 WxMpXmlOutTextMessage text=WxMpXmlOutTextMessage.TEXT(). toUser(message.getFromUser()). fromUser(message.getToUser()). content("你好,很高心認識你").build(); //轉化為xml格式 String xml=text.toXml(); System.out.println(xml); //返回消息 response.setCharacterEncoding("UTF-8"); PrintWriter out=response.getWriter(); out.print(xml); out.close(); } } /** * 首頁訪問 * @return */ @RequestMapping(value= "/",method = RequestMethod.GET) @ResponseBody public String index() { return "hello"; } }
3.編寫初始化文件:從application.yml配置文件中獲取 appId,secret,token,accesstoken,這幾個值來自公眾號
 
          
         package com.weixin.demo.demo.web.common.config; import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @ConfigurationProperties("siping") @Configuration public class WechatConfig { //從配置文件中獲取字段,需要設置get,set方法 private String appId; private String secret; private String token; private String accesstoken; /** * 初始化微信service * * @return */ @Bean public WxMpService getWxMpService() { WxMpService wxMpService = new WxMpServiceImpl(); WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage = new WxMpInMemoryConfigStorage(); wxMpInMemoryConfigStorage.setAppId(appId); wxMpInMemoryConfigStorage.setSecret(secret); wxMpInMemoryConfigStorage.setToken(token); wxMpInMemoryConfigStorage.setAccessToken(accesstoken); wxMpService.setWxMpConfigStorage(wxMpInMemoryConfigStorage); return wxMpService; } public String getAppId() { return appId; } public void setAppId(String appId) { this.appId = appId; } public String getSecret() { return secret; } public void setSecret(String secret) { this.secret = secret; } public String getToken() { return token; } public void setToken(String token) { this.token = token; } public String getAccesstoken() { return accesstoken; } public void setAccesstoken(String accesstoken) { this.accesstoken = accesstoken; } }
3.注意:
在回復消息的時候需要注意:toUser,和fromUser的主體是誰,一般時候微信端傳遞過來的值反着的。

4.通過natapp配置內網穿透映射127.0.0.1:8080端口,讓微信端能夠訪問到:
登錄注冊natapp,下載natapp的客戶端,運行,在命令行輸入:natapp -authtoken=自己登錄后分配的natapptoken號

5.和公眾號平台對接自己的服務器

6.結果

