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";
} }

weixinController

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;
}
}

WechatConfig

3.注意:

在回复消息的时候需要注意:toUser,和fromUser的主体是谁,一般时候微信端传递过来的值反着的。

4.通过natapp配置内网穿透映射127.0.0.1:8080端口,让微信端能够访问到:
登录注册natapp,下载natapp的客户端,运行,在命令行输入:natapp -authtoken=自己登录后分配的natapptoken号

5.和公众号平台对接自己的服务器

6.结果

												

微信开发基于springboot的更多相关文章

  1. 接口开发-基于SpringBoot创建基础框架

    说到接口开发,能想到的开发语言有很多种,像什么Java啊..NET啊.PHP啊.NodeJS啊,太多可以用.为什么选择Java,究其原因,最后只有一个解释,那就是“学Java的人多,人员招聘范围大,有 ...

  2. 基于spring-boot的社区社交微信小程序,适合做脚手架、二次开发

    基于spring-boot的社区社交微信小程序,适合做脚手架.二次开发 代码地址如下:http://www.demodashi.com/demo/13867.html 1 概述 笔者做的一个后端基于s ...

  3. 基于SpringBoot的开源微信开发平台,Jeewx-Boot 1.0 版本发布

    项目介绍 JeewxBoot 是一款基于SpringBoot的免费微信开发平台.支持微信公众号.小程序官网.微信抽奖活动. Jeewx-Boot实现了微信公众号管理.小程序CMS.微信抽奖活动等基础功 ...

  4. 基于SpringBoot从零构建博客网站 - 技术选型和整合开发环境

    技术选型和整合开发环境 1.技术选型 博客网站是基于SpringBoot整合其它模块而开发的,那么每个模块选择的技术如下: SpringBoot版本选择目前较新的2.1.1.RELEASE版本 持久化 ...

  5. Jeewx-Boot 1.1 版本发布,基于SpringBoot的开源微信管家系统

    项目介绍 JeewxBoot是一款基于SpringBoot的开源微信管家系统,采用SpringBoot2.1.3 + Mybatis + Velocity 框架技术.支持微信公众号.微信第三方平台(扫 ...

  6. JeecgBoot 2.1.1 代码生成器AI版本发布,基于SpringBoot+AntDesign的JAVA快速开发平台

    此版本重点升级了 Online 代码生成器,支持更多的控件生成,所见即所得,极大的提高开发效率:同时做了数据库兼容专项工作,让 Online 开发兼容更多数据库:Mysql.SqlServer.Ora ...

  7. 在线Online表单来了!JeecgBoot 2.1 版本发布——基于SpringBoot+AntDesign的快速开发平台

    项目介绍 Jeecg-Boot 是一款基于SpringBoot+代码生成器的快速开发平台! 采用前后端分离架构:SpringBoot,Ant-Design-Vue,Mybatis,Shiro,JWT. ...

  8. 基于SpringBoot免费开源的微信管家平台,Jeewx-Boot 1.0.3 版本发布

    项目介绍 JeewxBoot 是一款基于SpringBoot的免费微信开发平台.支持微信公众号.小程序官网.微信抽奖活动.Jeewx-Boot实现了微信公众号管理.小程序CMS.微信抽奖活动等基础功能 ...

  9. 基于SpringBoot+AntDesign的快速开发平台,JeecgBoot 2.0.2 版本发布

    Jeecg-Boot 是一款基于SpringBoot+代码生成器的快速开发平台! 采用前后端分离架构:SpringBoot,Ant-Design-Vue,Mybatis,Shiro,JWT. 强大的代 ...

随机推荐

  1. # 20175333曹雅坤《Java程序设计》第1周学习总结

    教材学习内容总结 1.学习第一章PPT,安装JRE,JDK并配置path环境参数 2.在windows上使用dos命令运行教材第一章代码Hello.java和People.java 3.下载使用git ...

  2. C语言可重入函数和不可重入函数

    可重入函数和不可重入函数的概念 在函数中如果我们使用静态变量了,导致产生中断调用别的函数的 过程中可能还会调用这个函数,于是原来的 静态变量被在这里改变了,然后返回主体函数,用着的那个静态变量就被改变 ...

  3. 使用Python进行OCR -- 识别图片中的文字

    工具 Tesseract pytesseract tesserocr 朋友需要一个工具,将图片中的文字提取出来.我帮他在网上找了一些OCR的应用,都不好用.所以准备自己研究,写一个Web APP供他使 ...

  4. 【转载】Vue自定义指令实现pc端加载更多

    转载来源:https://www.86886.wang/detail/5a6f19e644f9da55274c3bbd,谢谢作者分享! 原理 document.documentElement.scro ...

  5. iOS 新建xib文件时,最外层view的约束问题

    今天用在利用xib实例化view 时, 生成的view的自动布局总是用问题.具体来说,宽和高都不能和父view正确变化.仔细检查,发现下图: 注意这里右上角的Autoresizing部分,并没有设置正 ...

  6. tp5分页后数据处理

  7. linux关于软件安装的博文

    https://www.cnblogs.com/kundeg/archive/2018/03/06/7247934.html https://www.cnblogs.com/qiaozhoulin/p ...

  8. 金蝶K3 WISE BOM多级展开_物料齐套表

    /****** Object: StoredProcedure [dbo].[pro_bobang_ICItemQiTao] Script Date: 07/29/2015 16:12:10 **** ...

  9. ELK统一日志系统的应用

    收集和分析日志是应用开发中至关重要的一环,互联网大规模.分布式的特性决定了日志的源头越来越分散, 产生的速度越来越快,传统的手段和工具显得日益力不从心.在规模化场景下,grep.awk 无法快速发挥作 ...

  10. 代码生成工具更新--快速生成Winform框架的界面项目

    在之前版本的代码生成工具Database2Sharp中,由于代码生成都是考虑Winform和Web通用的目的,因此Winform界面或者Web界面都是单独生成的,在工具中生成相应的界面后,复制到项目里 ...