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

简介

项目搭建已在博主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方法形式。
这里写图片描述
这里写图片描述
到此微信公众号接入已开发完成。项目源码

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值