The background,
In the last section, we know how to access the WeChat official account, but then the official account will interact with us, so how to WeChat official account to inform our own server? We know that the URL we provide when accessing is GET/MP/Entry, then the events generated after the public number will be sent to our own server by POST/MP/Entry.
Two, code implementation, here or use the Weixin-Java-MP framework to achieve
1. Introducing Weixin-Java-MP
< the dependency > < groupId > com. Making. Binarywang < / groupId > < artifactId > weixin - Java - mp < / artifactId > < version > 4.0.0 < / version > </dependency>
2. Write the configuration
@Configuration public class WxMpConfiguration { @Autowired private WxMpProperties wxMpProperties; @Autowired private SubscribeHandler subscribeHandler; @Autowired private UnsubscribeHandler unsubscribeHandler; @Bean public WxMpService wxMpService() { WxMpServiceImpl wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(wxMpConfigStorage()); No. / / set up multiple WeChat public configuration / / wxMpService setMultiConfigStorages (); return wxMpService; } /** * This local configuration is saved locally, the production environment needs to extend itself, * * @return wxmpConfigStorage */ public wxmpConfigStorage wxmpConfigStorage () {wxmpDefaultConfigImpl storage = new WxMpDefaultConfigImpl(); storage.setAppId(wxMpProperties.getAppId()); storage.setSecret(wxMpProperties.getAppSecret()); storage.setAesKey(wxMpProperties.getAesKey()); storage.setToken(wxMpProperties.getToken()); return storage; } @Bean public WxMpMessageRouter messageRouter(WxMpService wxMpService) { WxMpMessageRouter router = new WxMpMessageRouter(wxMpService); / / message to heavy router. SetMessageDuplicateChecker (new WxMessageInMemoryDuplicateChecker ()); Router.rule ().async(false).msgType(EVENT).event(SUBSCRIBE).handler(this.subscribeHandler).end(); Router.rule ().async(false).msgType(EVENT).event(UNSUBSCRIBE).handler(this.unsubscribeHandler).end(); return router; }}
1. The message called back to us by the public account will be processed by WXMPMessageRouter. 2. During the configuration, we need to do a good job of message reprocessing. If our server 5S fails to respond to WeChat server, WeChat server will retry for 3 times. 3. Implement the WXMPMessageHandler interface for processing messages
3. Recall entry port
/** * WeChat callback ** @Param requestBody * @Param signature * @Param timestamp * @Param nonce * @Param openID * @Param encType * @param msgSignature * @return */ @PostMapping("/mp/entry") public String entryCallback(@RequestBody String requestBody, @RequestParam("signature") String signature, @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce, @RequestParam("openid") String openid, @RequestParam(name = "encrypt_type", required = false) String encType, @RequestParam(name = "msg_signature", Required = false) String msgSignature) {log.info("\n Receive WeChat request: [openid=[{}], [signature=[{}], encType=[{}], msgSignature=[{}]," + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ", openid, signature, encType, msgSignature, timestamp, nonce, RequestBody); the if (! WxMpService. CheckSignature (timestamp, nonce, signature)) {throw new IllegalArgumentException (" illegal request, ");} String out = null; if (encType == null) {// Clear text transmission message WXMPXMLMessage InMessage = WxMpXmlMessage.fromXml(requestBody); WxMpXmlOutMessage outMessage = this.wxMpMessageRouter.route(inMessage); if (outMessage == null) {return "";} out = outMessage.ToXML ();} else if ("aes".equalSignoRecase (encType)) {// AES encrypted message WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody, wxMpService.getWxMpConfigStorage(), timestamp, Nonce, msgSignature); log.info("\n message decrypted with: \n[{}] ", inMessage.toString()); WxMpXmlOutMessage outMessage = this.wxMpMessageRouter.route(inMessage); if (outMessage == null) { Return ""} out = outMessage. ToEncryptedXml (wxMpService. GetWxMpConfigStorage ());} the info (" \ n assembly reply information: [{}]", out); return out; }
3. Reference documents
1, the public number message encryption and decryption 2, receive ordinary messages 3, receive event messages
Four, code implementation
https://gitee.com/huan1993/wechat-development