Home  >  Q&A  >  body text

java - springmvc 如何配置微信公众号服务器controller。

调用接口发送消息成功,那服务器配置肯定是对的,但是收不到微信推送的消息,比如关注事件。

    @RequestMapping(value = "/", method = {RequestMethod.GET})
    public String wx(@RequestParam String signature, @RequestParam String timestamp, @RequestParam String nonce, String echostr, HttpServletRequest request, HttpServletResponse response) {
        if (!wxMpService.checkSignature(timestamp, nonce, signature)) {
            LOGGER.info("非法请求, signature:{}", signature);
            return "非法请求";
        }

        if (StringUtils.isNotBlank(echostr)) {
            LOGGER.info("验证:{}", echostr);
            return echostr;
        }

        LOGGER.info("wx:", JSON.toJSONString(request));
        return null;
    }

    @RequestMapping(value = "/", method = {RequestMethod.POST})
    public String service( HttpServletRequest request, HttpServletResponse response) throws Exception {
        LOGGER.info("service, request: {},", JSON.toJSONString(request));

        WxMpXmlMessage inMessage = WxMpXmlMessage.fromXml(request.getInputStream());
        WxMpXmlOutMessage outMessage = wxMpMessageRouter.route(inMessage);
        if(outMessage == null) {
            LOGGER.info("outMessage is null");
            return "";
        }
        return outMessage.toXml();
    }
    

get请求验证配置是通过的,post请求接不到数据,求问题所在,谢谢了

迷茫迷茫2715 days ago911

reply all(5)I'll reply

  • 迷茫

    迷茫2017-04-17 17:55:06

    Obviously you specified method = {RequestMethod.GET}

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:55:06

    produces:指定返回的内容类型,仅当requestThe specified type will be returned only if the (Accept) type in the request header contains the specified type;

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:55:06

    I haven’t tried the effect of request.getInputStream. Here’s what I wrote:

    @ResponseBody
    @RequestMapping(value = "/wechat-listener", method = RequestMethod.POST, produces = "application/xml")
    public Message onResponse(HttpEntity<RequestMessage> httpEntity) {
    
        RequestMessage requestMessage = httpEntity.getBody();
    
        logger.info("from openid: " + requestMessage.getFrom() +
                    "\n type: " + requestMessage.getType());
    
        return requestMessageHandler.handle(requestMessage);
    
    }
    
    

    I just searched it for you, and it’s not possible to use inputStream because spring-mvc has already consumed it.
    Reference http://hw1287789687.iteye.com/blog/2199295

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:55:06

    Thank you everyone for your answers. The problem has been solved. It was because the use of securityCROS in the project was restricted and the request was blocked. There is no problem with the writing method

    reply
    0
  • 迷茫

    迷茫2017-04-17 17:55:06

    Hello, I also encountered the same problem as you. What is the specific search idea? I have not configured security, so where should I start to solve the problem when I cannot receive requests? My email: jtmjx@163.com, QQ: 253552550, please name me, thank you.

    reply
    0
  • Cancelreply