您的位置:首页 > 移动开发 > 微信开发

JAVA微信公众号开发第1篇之环境配置与开发接入

2018-01-26 16:05 671 查看
简介

环境配置
工具jar包引用

配置集成微信工具

微信公众号参数配置

开发接入
主入口接口

接入

简介

项目搭建已在博主Eclipse+Maven创建多模块web项目博文中介绍,本文主要介绍微信模块的环境配置和开发的接入(PS:公众号申请和开发配置中已在博主微信公众号开发之微信测试账号申请博文中介绍,此处不做赘述)。

环境配置

工具jar包引用

将下方jar包引用代码贴在pom.xml中

<!-- 微信 jar-start -->
<dependency>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-common</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>me.chanjar</groupId>
<artifactId>weixin-java-tools</artifactId>
<version>1.0.2</version>
</dependency>


配置集成微信工具

本文SpringMVC框架配置采用JavaConfig方式配置详情请查阅博主SpringMVC入门项目搭建JavaConfig配置方式(零XML)博文

PS:@Bean注解用于告诉方法,产生一个Bean对象,然后这个Bean对象交给Spring管理。产生这个Bean对象的方法Spring只会调用一次,随后这个Spring将会将这个Bean对象放在自己的IOC容器中。使用@Bean注解的好处就是能够动态获取一个Bean对象,能够根据环境不同得到不同的Bean对象。或者说将Spring和其他组件分离(其他组件不依赖Spring,但是又想Spring管理生成的bean)。这样就可以通过@Autowired、@Resource、@Inject方式获取Bean。

private @Inject Environment environment;
/**
* @Title: wxMpInMemoryConfigStorage
* @Description: TODO 加载微信基本配置
* @return: WxMpInMemoryConfigStorage
*/
@Bean
public WxInMemoryConfigStorage wxInMemoryConfigStorage(){
WxInMemoryConfigStorage config = new WxInMemoryConfigStorage();
config.setAppId(environment.getProperty("wx.appId")); // 设置微信公众号的appid
config.setSecret(environment.getProperty("wx.appSecret")); // 设置微信公众号的app corpSecret
config.setToken(environment.getProperty("wx.token")); // 设置微信公众号的token
config.setAesKey(environment.getProperty("wx.aesKey")); // 设置微信公众号的EncodingAESKey
return config;
}
/**
* @Title: wxMpService
* @Description: TODO 微信API的Service
* @param: @param wxMpInMemoryConfigStorage
* @return: WxMpService
*/
@Bean
public WxService wxMpService(WxInMemoryConfigStorage wxInMemoryConfigStorage){
WxService wxService = new WxServiceImpl();
wxService.setWxConfigStorage(wxInMemoryConfigStorage);
return wxService;
}


微信公众号参数配置

wx.properties

#配置如下,不要填错了哦!注意大小写。
wx.appId=设置微信公众号的appid
wx.appSecret=设置微信公众号的app corpSecret
wx.token=设置微信公众号的token
wx.aesKey=设置微信公众号的EncodingAESKey
wx.systoken=公众号本地wxsystoken


开发接入

1.博主采用Spring RESTful风格url。目的传入参数wxsystoken做公众号区分(PS:多公众号接入时使用)

2.博主使用花生壳做域名解析将本地IP映射到外网详细请查阅花生壳域名解析博文

主入口接口

package com.bigbigbu.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import me.chanjar.weixin.api.WxService;

@RestController
@RequestMapping("wx")
public class WxController {
private @Inject WxService wxService;
@RequestMapping(value = "/wxconfig/{wxsystoken}", method = RequestMethod.GET)
public void wxconfigGET(@PathVariable("wxsystoken") String wxsystoken, HttpServletRequest request,
HttpServletResponse response) throws IOException {
// 验证服务器的有效性
PrintWriter out = response.getWriter();
String signature = request.getParameter("signature");
String timestamp = request.getParameter("timestamp");
String nonce = request.getParameter("nonce");
String echostr = request.getParameter("echostr");
// 验签操作
if (wxService.checkSignature(timestamp, nonce, signature)){
out.print(echostr);
}
}
@RequestMapping(value = "/wxconfig/{wxsystoken}", method = RequestMethod.POST)
public void wxconfigPOST(@PathVariable("wxsystoken") String wxsystoken, HttpServletRequest request,
HttpServletResponse response) throws IOException {
}
}


接入



点击接入是走的是wxconfigGET采用的GET方法形式,其余消息处理会进入wxconfigPOST采用POST方法形式。





到此微信公众号接入已开发完成。项目源码
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息