0

「GET」や「POST」などのリクエストを受け取るサーバー アプリケーションを Netty で作成する必要があります。GET リクエストの場合、パラメーターはクエリ パラメーターとして提供されます。

GET リクエストには HttpRequestDecoder が、投稿には HttpPostRequestDecoder が適していることを確認しています。しかし、どうすれば両方を同時に処理できますか?

Netty にはあまり詳しくないので、少し助けていただければ幸いです :)

4

2 に答える 2

2

netty は、パイプラインを一連のハンドラーとして定義するパイプラインとしてリクエストを処理するようにプロビジョニングします。

1 つのシーケンスは次のようになります。

p.addLast ("codec", new HttpServerCodec ());
p.addLast ("handler", new YourHandler());

ここで、p は ChannelPipeline インターフェイスのインスタンスです。YourHandler クラスは次のように定義できます。

public class YourHandler extends ChannelInboundHandlerAdapter
{
    @Override
    public void channelRead (ChannelHandlerContext channelHandlerCtxt, Object msg)
        throws Exception
    {
        // Handle requests as switch cases. GET, POST,...
        // This post helps you to understanding switch case usage on strings:
        // http://stackoverflow.com/questions/338206/switch-statement-with-strings-in-java
        if (msg instanceof FullHttpRequest)
        {
            FullHttpRequest fullHttpRequest = (FullHttpRequest) msg;
            switch (fullHttpRequest.getMethod ().toString ())
            {
                case "GET":
                case "POST":
                ...
            }
        }
    }
}
于 2013-10-17T17:58:58.943 に答える