Build a Netty HelloWorld Server in IntelliJ IDEA and Examine Core Components
This tutorial walks through creating a Netty HelloWorld server in IntelliJ IDEA using Gradle, adding the Netty dependency, implementing the server bootstrap, event loop groups, initializer and handler classes, and verifying the service with a curl request.
What is Netty
Netty is a Java NIO‑based asynchronous event‑driven network application framework that provides an API for building TCP and UDP client/server applications while also exposing low‑level APIs. It is fully asynchronous and widely used in projects such as Dubbo and Elasticsearch.
Project setup
Create a new Gradle project in IntelliJ IDEA, configure the JDK, and add the Netty dependency in build.gradle:
dependencies {
// https://mvnrepository.com/artifact/io.netty/netty-all
compile group: 'io.netty', name: 'netty-all', version: '4.1.52.Final'
}Gradle resolves the JAR after saving.
Server implementation
Create package com.badao.netty and class HelloWorldServer with a main method:
package com.badao.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class HelloWorldServer {
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new HelloWorldServerInitializer());
ChannelFuture channelFuture = serverBootstrap.bind(70).sync();
channelFuture.channel().closeFuture().sync();
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}The two EventLoopGroup instances follow Netty’s recommendation: bossGroup accepts incoming connections, workerGroup processes traffic.
Initializer
Create HelloWorldServerInitializer extending ChannelInitializer<SocketChannel> to configure the pipeline:
package com.badao.netty;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
public class HelloWorldServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast("httpServerCodec", new HttpServerCodec());
pipeline.addLast("helloWorldServerHandler", new HelloWorldServerHandler());
}
}Handler
Implement HelloWorldServerHandler extending SimpleChannelInboundHandler<HttpObject> to respond to HTTP requests:
package com.badao.netty;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
public class HelloWorldServerHandler extends SimpleChannelInboundHandler<HttpObject> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
if (msg instanceof HttpRequest) {
ByteBuf content = Unpooled.copiedBuffer("公众号:霸道的程序猿", CharsetUtil.UTF_8);
FullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
ctx.writeAndFlush(response);
}
}
}Running and verification
In .idea/gradle.xml set the option delegatedBuild to false so that the main method runs as a regular Java application rather than a Gradle task.
Execute the main method; the server listens on port 70. Verify with:
curl http://localhost:70The response body should be the plain‑text string “公众号:霸道的程序猿”, confirming that the Netty server is operational.
References
Netty version: 4.1.52.Final. Maven Central URL for the dependency: https://mvnrepository.com/artifact/io.netty/netty-all
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
The Dominant Programmer
Resources and tutorials for programmers' advanced learning journey. Advanced tracks in Java, Python, and C#. Blog: https://blog.csdn.net/badao_liumang_qizhi
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
