<dependency> <groupId>me.chanjar</groupId> <artifactId>weixin-java-mp</artifactId> <version>1.3.3</version> </dependency>
import java.io.InputStream; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlRootElement; import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage; import org.xml.sax.InputSource; @XmlRootElement(name = "xml") @XmlAccessorType(XmlAccessType.FIELD) class WxMpXMLInMemoryConfigStorage extends WxMpInMemoryConfigStorage { public static WxMpXMLInMemoryConfigStorage fromXml(InputStream is) throws JAXBException { Unmarshaller um = JAXBContext.newInstance( WxMpXMLInMemoryConfigStorage.class).createUnmarshaller(); InputSource inputSource = new InputSource(is); inputSource.setEncoding("utf-8"); return (WxMpXMLInMemoryConfigStorage) um.unmarshal(inputSource); } }
import java.io.InputStream; import me.chanjar.weixin.mp.api.WxMpConfigStorage; import me.chanjar.weixin.mp.api.WxMpMessageRouter; import me.chanjar.weixin.mp.api.WxMpService; import me.chanjar.weixin.mp.api.WxMpServiceImpl; public class WxMpServiceInstance { private WxMpService wxMpService; private WxMpConfigStorage wxMpConfigStorage; private WxMpMessageRouter wxMpMessageRouter; private static WxMpServiceInstance instance = null; public static WxMpServiceInstance getInstance() { if (instance == null) { try { instance = new WxMpServiceInstance(); } catch (Exception e) { e.printStackTrace(); } } return instance; } private WxMpServiceInstance() throws Exception { wxMpService = new WxMpServiceImpl(); // 讀取配置文件 InputStream inputStream = WxMpServiceInstance.class .getResourceAsStream("/config/weixin.xml"); wxMpConfigStorage = WxMpXMLInMemoryConfigStorage.fromXml(inputStream); wxMpService.setWxMpConfigStorage(wxMpConfigStorage); wxMpMessageRouter = new WxMpMessageRouter(wxMpService); } public WxMpService getWxMpService() { return wxMpService; } public WxMpConfigStorage getWxMpConfigStorage() { return wxMpConfigStorage; } public WxMpMessageRouter getWxMpMessageRouter() { return wxMpMessageRouter; } }
<xml> <!-- 微信appId --> <appId>123456789</appId> <!-- 微信secret --> <secret>123456789</secret> <!-- 商戶號 mch_id --> <partnerId>123456789</partnerId> <!-- 商戶Key --> <partnerKey>123456789</partnerKey> <!-- 微信token --> <token>123456789</token> <accessToken>123456789</accessToken> <!-- 微信aesKey --> <aesKey>123456789</aesKey> <oauth2redirectUri></oauth2redirectUri> </xml>
採用snsapi_base爲scope發起的網頁受權,是用來獲取進入頁面的用戶的openid的方式,具體請查看微信相關文檔html
String code = request.getParameter("code"); WxMpService wxMpService = WxMpServiceInstance.getInstance().getWxMpService(); // 獲取受權 WxMpOAuth2AccessToken oauth2AccessToken = wxMpService.oauth2getAccessToken(code); // 受權信息獲取用戶 WxMpUser wxMpUser = wxMpService.userInfo(oauth2AccessToken.getOpenId(),null);
這個只是部分的代碼,具體的說明,請親自查看源碼。java
這個框架很是不錯,除了wiki少了些外,但願更多牛人蔘與進入項目,再次感謝做者(chanjarster)。git
不知道什麼緣由,這個項目許久未維護了,因此在這個項目下有說明其分支的新的項目,QQ羣(343954419),新分支優化了不少,維護也積極,Demo和說明也全,建議你直接看源碼裏的說明,聰明的你確定喜歡上的;另外感謝爲該分支的貢獻者們,讓微信開發變得簡單。github