記SpringMVC項目weixin4j-0.0.9.2升級到weixin4j-spring-1.0.0、weixin4j-0.1.3過程

背景說明

項目採用maven創建。
由於之前項目中一直使用的是weixin4j-0.0.9.2版本,項目爲標準的Spring MVC + MyBatis + MySQL。
其中Spring MVC採用的是配置文件方式配置,下圖是項目大的結構。

原pom.xml引用如下:

<dependency>
    <groupId>org.weixin4j</groupId>
    <artifactId>weixin4j</artifactId>
    <version>0.0.9.2</version>
</dependency>

現改爲:

<dependency>
    <groupId>org.weixin4j</groupId>
    <artifactId>weixin4j-spring</artifactId>
    <version>1.0.0</version>
</dependency>

可能有些人還是爲手動加上

<dependency>
    <groupId>org.weixin4j</groupId>
    <artifactId>weixin4j</artifactId>
    <version>0.1.3</version>
</dependency>

其實這個不是必須的,因爲weixin4j-spring本身就已經引用了weixin4j所以不需要顯式添加了。

這次我沒采用xml去配置weixin4j-spring,而是採用了註解。
直接在項目目錄創建一個WebConfig配置註解類。

package com.ansitech.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.weixin4j.factory.WeixinFactory;
import org.weixin4j.loader.ITicketLoader;
import org.weixin4j.loader.ITokenLoader;
import org.weixin4j.spi.IEventMessageHandler;
import org.weixin4j.spi.INormalMessageHandler;
import org.weixin4j.spring.MessageFactoryBean;
import org.weixin4j.spring.WeixinFactoryBean;
import org.weixin4j.spring.WeixinTemplate;

/**
 * WebConfig
 *
 * @author yangqisheng
 */
@Configuration
public class WebConfig {

    @Bean
    public WeixinFactoryBean weixinFactoryBean(ITokenLoader tokenLoader, ITicketLoader ticketLoader) {
        System.out.println("...WebConfig.WeixinFactoryBean...");
        WeixinFactoryBean weixinFactoryBean = new WeixinFactoryBean();
        weixinFactoryBean.setTokenLoader(tokenLoader);
        weixinFactoryBean.setTicketLoader(ticketLoader);
        return weixinFactoryBean;
    }

    @Bean
    public WeixinTemplate weixinTemplate(WeixinFactory weixinFactory) {
        System.out.println("...WebConfig.WeixinTemplate...");
        return new WeixinTemplate(weixinFactory);
    }

    @Bean
    public MessageFactoryBean messageFactoryBean(INormalMessageHandler normalMessageHandler, 
                                                 IEventMessageHandler eventMessageHandler) {
        System.out.println("...WebConfig.MessageFactoryBean...");
        MessageFactoryBean messageFactoryBean = new MessageFactoryBean();
        messageFactoryBean.setNormalMessageHandler(normalMessageHandler);
        messageFactoryBean.setEventMessageHandler(eventMessageHandler);
        return messageFactoryBean;
    }
}

接着就是去改造項目代碼了,首先是控制器中原代碼的調用。
原項目中採用OAuth2進行網頁授權的代碼是最先要改的。
原項目代碼(片段):

import org.weixin4j.Configuration;
import org.weixin4j.OAuth2;
...
//初始化當前用戶身份,如果獲取到身份,則重新跳轉到當前頁面
if (!isOAuth(request)) {
    //跳轉到統一獲取用戶身份的地方
    String url = Configuration.getProperty("weixin4j.oauth.url") + "/mobile/api/oauth/index/" 
                 + activity.getString("activityid");
    OAuth2 oauth = new OAuth2();
    String redirect_url = oauth.getOAuth2CodeBaseUrl(Configuration.getOAuthAppId(), url);
    //進行跳轉進行網頁靜默授權
    response.sendRedirect(redirect_url);
    return null;
}

首先需要先引入WeixinTemplate:

@Autowired
private WeixinTemplate weixinTemplate;

然後改造代碼內容:

import org.weixin4j.Configuration;
import org.weixin4j.spring.WeixinTemplate;
//初始化當前用戶身份,如果獲取到身份,則重新跳轉到當前頁面
if (!isOAuth(request)) {
    //跳轉到統一獲取用戶身份的地方
    String url = Configuration.getProperty("weixin4j.oauth.url") + "/mobile/api/oauth/index/" 
                 + activity.getString("activityid");
    String redirect_url = weixinTemplate.sns().getOAuth2CodeBaseUrl(url);
    //進行跳轉進行網頁靜默授權
    response.sendRedirect(redirect_url);
    return null;
}

最後是根據code獲取openid的地方需要進行修改。
下面是原代碼片段:

import org.weixin4j.OAuth2;
import org.weixin4j.WeixinException;
import org.weixin4j.http.OAuth2Token;
...
OAuth2 oauth = new OAuth2();
OAuth2Token oAuth2Token = oauth.login(Configuration.getOAuthAppId(), 
                             Configuration.getOAuthSecret(), code);
//靜默授權獲取用戶身份
String openid = oAuth2Token.getOpenid();

改爲新的後爲:

import org.weixin4j.WeixinException;
import org.weixin4j.spring.WeixinTemplate;
...
//靜默授權獲取用戶身份
String openid = weixinTemplate.sns().getOpenId(code);

至此升級就完成了,對於其他的組件,也可以按此步驟更換。
新版的代碼是不是簡潔很多啊,o(∩_∩)o 哈哈
涉及商業,代碼不便上傳,請大家自行選擇是否升級。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章