【问题标题】:HttpServletRequest's body is empty when war is deployed to AWS将战争部署到 AWS 时,HttpServletRequest 的正文为空
【发布时间】:2013-12-23 08:51:05
【问题描述】:

在我的本地机器上运行时,我发现我的代码行为不一致。 AWS 弹性豆茎。该代码用于接收来自 Google Glass 的通知。

代码 sn-p 如下所示:

public class NotifyServlet extends HttpServlet implements WeiboConstants{
    private static final Logger LOG = Logger.getLogger(NotifyServlet.class.getSimpleName());

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        // Respond with OK and status 200 in a timely fashion to prevent redelivery
        response.setContentType("text/html");
        Writer writer = response.getWriter();
        writer.append("OK");
        writer.close();

        LOG.info("perpare reading");

        BufferedReader notificationReader =
            new BufferedReader(new InputStreamReader(request.getInputStream()));
        String notificationString = "";

        LOG.info("begin reading ");
        // Count the lines as a very basic way to prevent Denial of Service attacks
        int lines = 0;
        while (notificationReader.ready()) {
            LOG.info("reading");
            notificationString += notificationReader.readLine();
            lines++;

            // No notification would ever be this long. Something is very wrong.
            if (lines > 1000) {
                throw new IOException("Attempted to parse notification payload that was unexpectedly long.");
            }
        }

        LOG.info("got raw notification " + notificationString);

        JsonFactory jsonFactory = new JacksonFactory();

Google的服务器会做POST,单独注册:

Subscription subscription = MirrorClient.insertSubscription(credential, "https://mirrornotifications.appspot.com/forward?url=http://foo.elasticbeanstalk.com/notify", userId,
                        "timeline");

在本地机器上进行测试时,我运行了 mvn jetty:run 并将上面代码中的 foo 更改为 my ip address:8080。

我可以收到来自 google 的通知:

{ “集合”:“时间线”,“itemId”:“5cbf8f54-822d-4a8e-9a9f-508687bbb9e2”,“操作”:“INSERT”,“userToken”:“105242333117256730066”,“userActions”:[{ “类型”:“启动”}]}

但是当我将war (mvn war:war) 打包并部署到AWS 时,从日志中可以看到它已经成功注册到Google,并且我得到了来自Google 的POST在 /notify 上,但似乎正文是空的,如以下日志所示:

Dec 06, 2013 5:01:55 AM com.google.glassware.NotifyServlet doPost
INFO: perpare reading
Dec 06, 2013 5:01:55 AM com.google.glassware.NotifyServlet doPost
INFO: begin reading 
Dec 06, 2013 5:01:55 AM com.google.glassware.NotifyServlet doPost
INFO: got raw notification 
Dec 06, 2013 5:01:55 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [notify] in context with path [] threw exception
java.lang.IllegalArgumentException: no JSON input found

如何进一步调试?

更新

我将相同的 war 文件部署到运行 Jetty 而不是 Tomcat 的服务器上,它工作正常,似乎是 Tomcat 特定的问题?

【问题讨论】:

    标签: java maven amazon-web-services amazon-elastic-beanstalk google-glass


    【解决方案1】:

    要进一步调试,您可以尝试将通知直接发布到http://foo.elasticbeanstalk.com/notify

    如果这导致相同的行为,我会仔细查看 BufferedReader.ready() 作为循环条件。

    ready() 是一种非阻塞方法。这意味着如果由于某种原因BufferedReader 的实例没有准备好立即返回数据,那么该方法将立即返回 false 并且将完全跳过循环。

    那么调试的下一步就是注释掉ready() 并直接阻止readLine() 以确认您的servlet 接收到payload。

    更新

    似乎 Jetty 和 Tomcat 确实对 BufferedReader.ready() 有不同的实现。

    这是我在进一步研究后发现的相关 Stack Overflow 问题:BufferedReader ready method

    【讨论】:

    • 谢谢安德烈-我确实尝试过自己发布,而不是从谷歌那里收到它,这仍然是重复的。我会做更多的调查和报告。
    • 实际上,看看这个 SO 问题/答案:stackoverflow.com/questions/17510690/…
    猜你喜欢
    • 1970-01-01
    • 2018-10-30
    • 2014-10-30
    • 2014-03-18
    • 2017-02-24
    • 2017-09-20
    • 1970-01-01
    • 2016-09-19
    • 1970-01-01
    相关资源
    最近更新 更多