参考文章 https://blog.csdn.net/yzh_1346983557/article/details/84769914
接下来贴上我的学习过程,加了一些补充,如果这套看不懂的同学推荐先看我的这篇博文:https://blog.csdn.net/qq_41850449/article/details/88864219
但是后来我发现这样写太冗余了,大家也可以看我这篇
https://blog.csdn.net/qq_41850449/article/details/88992460
-
添加依赖并且消除Tomcat依赖
<dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.31.Final</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> //该布为消除tomcat依赖 </exclusion> </exclusions> </dependency>
-
建一个EchoServer类
public class EchoServer { private final int port; private Logger log = LoggerFactory.getLogger(this.getClass()); public EchoServer(int port) { this.port=port; } public void start() throws Exception{ EventLoopGroup group = new NioEventLoopGroup(); try { ServerBootstrap serverBootstrap=new ServerBootstrap(); serverBootstrap.group(group) .localAddress(new InetSocketAddress(port)) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { channel.pipeline().addLast(new HttpRequestDecoder()); channel.pipeline().addLast(new HttpObjectAggregator(65535)); channel.pipeline().addLast(new HttpResponseEncoder()); channel.pipeline().addLast(new HttpHandler()); } //顺序别乱写 }); ChannelFuture f=serverBootstrap.bind().sync(); f.channel().closeFuture().sync(); } finally { group.shutdownGracefully().sync(); } } }
-
建一个HttpHandler类
@ChannelHandler.Sharable public class HttpHandler extends SimpleChannelInboundHandler<Object> { @Override protected void channelRead0(ChannelHandlerContext ctx, Object obj) throws Exception { FullHttpResponse response = new DefaultFullHttpResponse( HTTP_1_1, OK, Unpooled.wrappedBuffer("OK OK OK OK".getBytes())); response.headers().set(CONTENT_TYPE, "text/plain"); //返回是字符串 //允许跨域访问 response.headers().set(ACCESS_CONTROL_ALLOW_ORIGIN,"*"); response.headers().set(ACCESS_CONTROL_ALLOW_HEADERS,"Origin, X-Requested-With, Content-Type, Accept"); response.headers().set(ACCESS_CONTROL_ALLOW_METHODS,"GET, POST, PUT,DELETE"); response.headers().set(CONTENT_LENGTH, response.content().readableBytes()); response.headers().set(CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.write(response); ctx.flush(); FullHttpRequest fullHttpRequest=(FullHttpRequest) obj; // 将GET, POST所有请求参数转换成Map对象 Map<String, String> parmMap = new RequestParser(fullHttpRequest).parse(); System.out.println(parmMap); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { //cause.printStackTrace(); ctx.close(); System.out.println("error"); }
-
参数解析类:
public class RequestParser { private FullHttpRequest fullReq; /** * 构造一个解析器 * @param req */ public RequestParser(FullHttpRequest req) { this.fullReq = req; } /** * 解析请求参数 * @return 包含所有请求参数的键值对, 如果没有参数, 则返回空Map * * @throws * @throws IOException */ public Map<String, String> parse() throws Exception, IOException { HttpMethod method = fullReq.method(); Map<String, String> parmMap = new HashMap<>(); if (HttpMethod.GET == method) { // 是GET请求 QueryStringDecoder decoder = new QueryStringDecoder(fullReq.uri()); decoder.parameters().entrySet().forEach( entry -> { // entry.getValue()是一个List, 只取第一个元素 parmMap.put(entry.getKey(), entry.getValue().get(0)); }); } else if (HttpMethod.POST == method) { // 是POST请求 HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(fullReq); decoder.offer(fullReq); List<InterfaceHttpData> parmList = decoder.getBodyHttpDatas(); for (InterfaceHttpData parm : parmList) { Attribute data = (Attribute) parm; parmMap.put(data.getName(), data.getValue()); } } else { // 不支持其它方法 // throw new MethodNotSupportedException(""); // 这是个自定义的异常, 可删掉这一行 } return parmMap; } }
-
最后的一步。
@SpringBootApplication public class HttpWebsocketApplication implements CommandLineRunner { public static void main(String[] args) { SpringApplication.run(HttpWebsocketApplication.class, args); } @Override public void run(String... strings) throws Exception { EchoServer echoServer = new EchoServer(7000); echoServer.start(); } }
-
这样就完成啦