gpt4 book ai didi

java - RequestHandler 未在 Netty 服务器中调用

转载 作者:行者123 更新时间:2023-11-30 07:41:55 25 4
gpt4 key购买 nike

我在 Netty Http 服务器上工作。我创建并注册了处理程序。但是我没有看到请求命中处理程序

这是主类

public class NettyServer {

private int port;

private NettyServer(int port) {
this.port = port;
}

public static void main(String[] args) throws Exception {
int port;
if (args.length > 0) {
port = Integer.parseInt(args[0]);
} else {
port = 8080;
}
new NettyServer(port).run();
}

private void run() throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new HttpMessageHandler(),new CalculatorOperationHandler());
}
}).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true);

ChannelFuture f = b.bind(port).sync();
f.channel().closeFuture().sync();
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
}
}

HttpMessageHandler.java

public class HttpMessageHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) throws Exception {
System.out.println("hello");
String uri = msg.uri();
HttpMethod httpMethod = msg.method();
HttpHeaders headers = msg.headers();

if (HttpMethod.GET == httpMethod) {

String[] uriComponents = uri.split("[?]");
String endpoint = uriComponents[0];
String[] queryParams = uriComponents[1].split("&");

if ("/calculate".equalsIgnoreCase(endpoint)) {

String[] firstQueryParam = queryParams[0].split("=");
String[] secondQueryParam = queryParams[1].split("=");

Integer a = Integer.valueOf(firstQueryParam[1]);
Integer b = Integer.valueOf(secondQueryParam[1]);
String operator = headers.get("operator");

Operation operation = new Operation(a, b, operator);
ctx.fireChannelRead(operation);
}
} else {
throw new UnsupportedOperationException("HTTP method not supported");
}

}
}

当我调用 localhost:8080/calculate?a=1&b=2

时,我没有看到控制台打印“hello”

这里有什么问题吗?

最佳答案

您的问题是由管道中缺少处理程序引起的。

目前,您的管道中只有 2 个处理程序:

  • HttpMessageHandler,处理 FullHttpRequest 对象
  • CalculatorOperationHandler,处理 Operation 对象

当来自浏览器的数据进来时,它以 ByteBuf 对象的形式进来,但你不处理这个对象!

要将 ByteBuf 转换为 FullHttpRequest,您需要在您的管道中添加其他可以执行此操作的处理程序。

您需要的第一个处理程序是 HttpServerCodec,此类将 ByteBuf 对象转换为作为 HTTP 交换的一部分的对象,如 header 、尾随 header 和请求主体。

然后你需要添加一个HttpObjectAggregator,它将上述对象组合成一个FullHttpRequest,所以你只需要处理1个对象。

ch.pipeline().addLast(
new HttpServerCodec(),
new HttpObjectAggregator(65536), // Handle POST/PUT requests up 64KB
new HttpMessageHandler(),
new CalculatorOperationHandler()
);

如果您想查看任何层之间的流量,您还可以添加一个 new LoggingHandler(LogLevel.INFO)

关于java - RequestHandler 未在 Netty 服务器中调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55425610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com