赞
踩
技术交流群:958923746,有学习视频,文档等。
一、基于上一篇文章已经说了如何配置服务器,配置完成后给公众号发送消息会返回接收的消息内容,一个json串。
二、打开上一篇提供的项目地址,打开WechatController
代码如下:这个代码是我经过接收的消息处理过的,使用的是加密后的消息,经过解密后得到一个XML消息串。
从这里看的出来,在验证tokent的时候的链接与接收消息的链接是一致的,
验证token是GET请求,会自动在GET方法中处理token,进行验证。@GetMapping
处理消息的时候是POST,会在POST方法中处理消息。@PostMapping
- package com.qh.wechat.controller;
-
- import me.chanjar.weixin.mp.api.WxMpMessageRouter;
- import me.chanjar.weixin.mp.api.WxMpService;
- import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
- import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
- import org.apache.commons.lang3.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import com.alibaba.fastjson.JSONObject;
- import com.qh.wechat.builder.TextBuilder;
- import com.qh.wechat.service.QhChatService;
- import com.qh.wechat.utils.JsonUtils;
- import com.qh.wechat.utils.StringHelpers;
-
- /**
- * @author Binary Wang(https://github.com/binarywang)
- */
- @RestController
- @RequestMapping("/wechat/portal")
- public class WechatController {
- private final Logger logger = LoggerFactory.getLogger(this.getClass());
-
- @Autowired
- private WxMpService wxService;
-
- @Autowired
- private WxMpMessageRouter router;
-
- @Autowired
- private QhChatService qhChatService;
-
- @GetMapping(produces = "text/plain;charset=utf-8")
- public String authGet(@RequestParam(name = "signature", required = false) String signature,
- @RequestParam(name = "timestamp", required = false) String timestamp,
- @RequestParam(name = "nonce", required = false) String nonce,
- @RequestParam(name = "echostr", required = false) String echostr) {
-
- this.logger.info("\n接收到来自微信服务器的认证消息:[{}, {}, {}, {}]", signature, timestamp, nonce, echostr);
-
- if (StringUtils.isAnyBlank(signature, timestamp, nonce, echostr)) {
- throw new IllegalArgumentException("请求参数非法,请核实!");
- }
-
- if (this.wxService.checkSignature(timestamp, nonce, signature)) {
- return echostr;
- }
-
- return "非法请求";
- }
-
- @PostMapping(produces = "application/xml; charset=UTF-8")
- public String post(@RequestBody String requestBody, @RequestParam("signature") String signature,
- @RequestParam("timestamp") String timestamp, @RequestParam("nonce") String nonce,
- @RequestParam(name = "encrypt_type", required = false) String encType,
- @RequestParam(name = "msg_signature", required = false) String msgSignature) {
- // this.logger.info(
- // "\n接收微信请求:[signature=[{}], encType=[{}], msgSignature=[{}],"
- // + " timestamp=[{}], nonce=[{}], requestBody=[\n{}\n] ",
- // signature, encType, msgSignature, timestamp, nonce, requestBody);
-
- if (!this.wxService.checkSignature(timestamp, nonce, signature)) {
- throw new IllegalArgumentException("非法请求,可能属于伪造的请求!");
- }
- String out = null;
- if (encType == null) {
- // 明文传输的消息
- WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(requestBody);
- WxMpXmlOutMessage outMessage = this.route(inMessage);
- if (outMessage == null) {
- return "";
- }
-
- out = outMessage.toXml();
- } else if ("aes".equals(encType)) {
- this.logger.info("--------------------aes加密的消息");
- // aes加密的消息
- WxMpXmlMessage inMessage = WxMpXmlMessage.fromEncryptedXml(requestBody,
- this.wxService.getWxMpConfigStorage(), timestamp, nonce, msgSignature);
-
- // this.logger.debug("\n消息解密后内容为:\n{} ", inMessage.toString());
- // WxMpXmlOutMessage outMessage = this.route(inMessage);
- // if (outMessage == null) {
- // return "";
- // }
- JSONObject jsStr = JSONObject.parseObject(JsonUtils.toJson(inMessage));
- try {
- out = qhChatService.qhChat(jsStr);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
- // this.logger.info(out+"-----------调用接口返回数据");
- WxMpXmlOutMessage build = new TextBuilder().build(out, inMessage, this.wxService);
- out = build.toEncryptedXml(this.wxService.getWxMpConfigStorage());
-
- // 组装XML返回给微信
- // out = outMessage
- // .toEncryptedXml(this.wxService.getWxMpConfigStorage());
- }
-
- this.logger.debug("\n组装回复信息:{}", out);
- return out;
- }
-
- private WxMpXmlOutMessage route(WxMpXmlMessage message) {
- try {
- return this.router.route(message);
- } catch (Exception e) {
- this.logger.error(e.getMessage(), e);
- }
-
- return null;
- }
-
- }

返回的消息也是一个XML,需要微信的API转成XML。调用该方法:
WxMpXmlOutMessage build = new TextBuilder().build(out, inMessage, this.wxService);
out = build.toEncryptedXml(this.wxService.getWxMpConfigStorage());
将需要回复的消息拼成一个微信可以识别的一个串,返回给微信。消息内容就是out。可以在上面处理out,然后将out重新转成XML。
最后效果:展示出来的是一个正常的消息,而不是一个json串。下面是我调用图灵接口进行消息的回复。
不懂可以留言联系。再会。
技术交流群:958923746,有学习视频,文档等。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。