Java检查GET请求信息

时间:2018-03-18 15:04:50

标签: java spring facebook spring-restcontroller ngrok

我正在使用Facebook Messenger应用程序(chatbot),我希望看到我收到的GET请求。我正在使用Spring Framework来启动http服务器和ngrok以使其在Facebook上可见。

Facebook向我发送webhooks并收到它们,但我不明白如何从此请求中提取数据。这是我在尝试 HttpRequest 接收GET请求时得到的结果。 ngrok screenshot(错误500)。 当我在没有HttpRequest的情况下尝试时,我的响应为200(确定)。

我需要为 find 方法的参数添加什么来查看GET请求数据?

我的代码:

@RestController
public class botAnswer {

    @RequestMapping(method= RequestMethod.GET)
    public String find(HttpRequest request) {
        System.out.println(request.getURI());
        String aaa = "222";

        return aaa;
    }
}

1 个答案:

答案 0 :(得分:0)

我猜HttpRequest对你没有帮助。为简单起见,只需将HttpRequest更改为HttpServletRequest即可。您可以使用request.getParameter("...")从中访问所有查询字符串参数。以下内容应该有效:

@RequestMapping(value = "/", method = RequestMethod.GET)
public String handleMyGetRequest(HttpServletRequest request) {
    // Reading the value of one specific parameter ...
    String value = request.getParameter("myParam");

    // or all parameters 
    Map<String, String[]> params = request.getParameterMap();

    ...
}

This blog post显示如何使用@RequestParam注释作为直接从HttpServletRequest读取参数的替代方法。