2016-04-07 10 views
0

私はJavaに埋め込まれたnetty 4.1を使用しており、パイプラインでクライアントのPOST要求からデータを取得しようとしています。私はいくつかのオプションを試しましたが、私はオンラインで見つかったが、何も動作しません...Java netty get POSTリクエストコンテンツ

多分誰かがこれについて有用な考えを持っています。

皆さん、ありがとうございました。

パイプライン:

p.addLast ("codec", new HttpServerCodec()); 
    p.addLast("decoder", new HttpRequestDecoder()); 
    p.addLast("encoder", new HttpRequestEncoder()); 
    p.addLast("handler",new InboundHandlerA()); 

ハンドラ:ネッティーを使用して

private static class InboundHandlerA extends ChannelInboundHandlerAdapter{ 
    @Override 
    public void channelActive(ChannelHandlerContext ctx) { 
     System.out.println("Connected!"); 
     ctx.fireChannelActive(); 
    } 

public void channelRead (ChannelHandlerContext channelHandlerCtxt, Object msg) throws Exception { 

     System.out.println(msg); 
    } 
    } 
+0

現時点では何がありますか?現在のハンドラとパイプラインはどのように見えますか? – Ferrybig

+0

@Ferrybig私は投稿を編集しました:)私は、POSTまたはGETのためのchannelReadでスイッチケースをしたいです – ekul

+0

@Ferrybig私の質問を指定することができます:どのメソッドを私はオーバーライドする必要がありますどのようなハンドラは、POSTコンテンツを取得する – ekul

答えて

5

Recieving HTTPリクエストでは、次のパイプラインでこれを行うことができ、簡単です:

// Provides support for http objects: 
p.addLast("codec", new HttpServerCodec()); 
// Deals with fragmentation in http traffic: 
p.addLast("aggregator", new HttpObjectAggregator(Short.MAX_VALUE)); 
// Deals with optional compression of responses: 
// p.addLast("aggregator", new HttpContentCompressor()); 
p.addLast("handler",new InboundHandlerA()); 

これを使用することができますカスタム付きSimpleChannelInboundHandler<FullHttpRequest>

public class InboundHandlerA extends SimpleChannelInboundHandler<FullHttpRequest> { 

    @Override 
    public void channelActive(ChannelHandlerContext ctx) { 
     super.channelActive(ctx); 
     System.out.println("Connected!"); 
    } 

    // Please keep in mind that this method 
     will be renamed to messageReceived(ChannelHandlerContext, I) in 5.0. 
    @Override 
    public void channelRead0 (ChannelHandlerContext ctx, 
           FullHttpRequest msg) throws Exception { 
     // Check for invalid http data: 
     if(msg.getDecoderResult() != DecoderResult.SUCCESS) { 
      ctx.close(); 
      return; 
     } 

     System.out.println("Recieved request!"); 
     System.out.println("HTTP Method: " + msg.getMethod()); 
     System.out.println("HTTP Version: " + msg.getProtocolVersion()); 
     System.out.println("URI: " + msg.getUri()); 
     System.out.println("Headers: " + msg.headers()); 
     System.out.println("Trailing headers: " + msg.trailingHeaders()); 

     ByteBuf data = msg.content(); 
     System.out.println("POST/PUT length: " + data.readableBytes()); 
     System.out.println("POST/PUT as string: "); 
     System.out.println("-- DATA --"); 
     System.out.println(data.toString(StandardCharsets.UTF_8)); 
     System.out.println("-- DATA END --"); 

     // Send response back so the browser won't timeout 
     ByteBuf responseBytes = ctx.alloc().buffer(); 
     responseBytes.writeBytes("Hello World".getBytes()); 

     FullHttpResponse response = new DefaultFullHttpResponse(
      HttpVersion.HTTP_1_1, HttpResponseStatus.OK, responseBytes); 
     response.headers().set(HttpHeaders.Names.CONTENT_TYPE, 
           "text/plain"); 
     response.headers().set(HttpHeaders.Names.CONTENT_LENGTH, 
           response.content().readableBytes()); 

     response.headers().set(HttpHeaders.Names.CONNECTION, 
           HttpHeaders.Values.KEEP_ALIVE); 
     ctx.write(response); 
    } 
} 

上記のコードは、投稿データを含め、着信メッセージのすべての詳細を印刷しています。投稿データのみが必要な場合は、単純なif文を追加して、POST応答タイプのフィルタリングを行うことができます

+0

説明ありがとう。私は今それが私を助けるかどうかを理解し、その後に答えを書くでしょう:) – ekul

+0

あなたが使用しているメソッドの多くは廃止予定とマークされています。私は "Connected!"メッセージはあるがそれ以上はない。おそらく、パイプラインはSocketChannelのchと最終的なChannelPipelineのp = ch.pipeline()によって作成されることを知っておくことが重要です。 – ekul

+0

今、msg.getMethod()を使用することができました。この問題はif文(if msg!getDecoderResult())のように見えます。私はそれをそのように働くことはできません。それは言う: "演算子は、引数の型DecoderResult"のために未定義です。私はtriet if(msg.getDecoderResult()!= null)の結果がない... – ekul