우리가 당신 핸들러의 순서로 파이프 라인을 정의하는 파이프 라인으로 요청을 처리 할 수있는 그물코의 규정을 준수해야합니다.
하나의 시퀀스는 다음과 같이 될 수있다 : p는
ChannelPipeline 인터페이스의 인스턴스
p.addLast ("codec", new HttpServerCodec());
p.addLast ("handler", new YourHandler());
. 다음과 같이 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":
...
}
}
}
}
이 방법을 시도했지만 msg를 FullHttpRequest로 매핑 할 수 없습니다. 채널 읽기는 여러 객체 (DefaultHttpRequest, LastHttpContent)로 여러 번 호출됩니다. –
지시문의 인스턴스를 사용하여 객체의 종류를 알 수 있습니다. if (msg instanceof FullHttpRequest) { FullHttpRequest fullHttpRequest = (FullHttpRequest) msg; } 더 명확하게하기 위해 내 대답에 코드 스 니펫을 업데이트했습니다! – Corehacker
파이프 라인에 [HttpRequestAggregator] (http://netty.io/4.1/api/io/netty/handler/codec/http/HttpObjectAggregator.html)를 추가하면 HttpMessage와 HttpContents를 집계하는 작업을 수행하게됩니다. '를'FullHttpRequest'로 변환합니다. – Matt