首頁  >  問答  >  主體

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 天前912

全部回覆(5)我來回復

  • 迷茫

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

    很明顯 你指定了 method = {RequestMethod.GET}

    回覆
    0
  • 伊谢尔伦

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

    produces:指定返回的内容类型,仅当request請求頭中的(Accept)類型中包含該指定類型才會回傳;

    回覆
    0
  • 迷茫

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

    沒試過request.getInputStream是什麼效果,我是這麼寫的:

    @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);
    
    }
    
    

    剛剛幫你search了一下,用inputStream是不行的,因為spring-mvc已經把它消耗過了。
    參考http://hw1287789687.iteye.com/blog/2199295

    回覆
    0
  • 迷茫

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

    謝謝大家回答了,問題已經解決了,是因為專案裡使用了securityCROS受限,請求被擋住了,寫法什麼的沒問題

    回覆
    0
  • 迷茫

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

    您好,我也遇到了和你一樣的問題,請問一下具體查找的思路是什麼?我沒有配置security,那麼我接收不到請求應該從哪塊下手解決呢?我的信箱:jtmjx@163.com,QQ:253552550,麻煩您幫我點名一下,謝謝。

    回覆
    0
  • 取消回覆