Java
jetty

JettyでIllegalStateExceptionが起きた話。

はじめに

JettyでPOSTデータを受け取ろうとしたら、様子が変だ。
確認してみよう。

環境

Jetty embedded

        <dependency>
            <groupId>org.eclipse.jetty</groupId>
            <artifactId>jetty-server</artifactId>
            <version>9.4.6.v20170531</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.eclipse.jetty.aggregate/jetty-all-server -->
        <dependency>
            <groupId>org.eclipse.jetty.aggregate</groupId>
            <artifactId>jetty-all-server</artifactId>
            <version>8.2.0.v20160908</version>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jetty.orbit</groupId>
            <artifactId>javax.servlet</artifactId>
            <version>3.0.0.v201112011016</version>
        </dependency>

すでに使える変数

Request baseRequest, HttpServletRequest request, HttpServletResponse response

ログ

p1p2p3p4[Thu Jun 14 00:29:21 JST 2018][LowerLobby]:null
p6p1p2p3java.lang.IllegalStateException: STREAMED
    at org.eclipse.jetty.server.Request.getReader(Request.java:1149)
    at com.haniokasai.mc.SteadyMistress.web.handlers.srvtools.pluginmanager_handler.pluginmanager_handler(pluginmanager_handler.java:59)
    at com.haniokasai.mc.SteadyMistress.web.WebHandler.handle(WebHandler.java:63)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
    at org.eclipse.jetty.server.Server.handle(Server.java:564)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
    at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
    at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
    at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
    at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)
    at org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)
    at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:199)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:673)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:591)
    at java.lang.Thread.run(Thread.java:748)

解決策

こいつは何だ?

割とすぐに記述が見つかった。
==>>http://apache-wicket.1842946.n4.nabble.com/java-lang-IllegalStateException-STREAMED-Consuming-JSON-fails-td3850442.html

This exception says "Don't use the reader but use the input stream
because something (maybe header) has been already read from the
stream".

この例外は"readerを使わないでinput streamを使え。何故なら、なにか(たぶんヘッダー)がstreamをすでに使ってるからだよ。"と表してるよ。

ほほ、そうかね。

つまり、すでに用意されているbufferreaderを使わなければいい。

BufferedReader in = baseRequest.getReader();

から

BufferedReader in = new BufferedReader( new InputStreamReader(request.getInputStream()));

にすればよい!って質問者が言ってるね!

以上!