经过前几结课程的学习,相信大家应该已经对gradle的基本使用有所了解了吧。从这节课开始教大家java的vert.x库的基本使用方法。
还记得我之前写的最基本的vert.x程序吗?就是那个只显示helloworld的程序。
import io.vertx.core.Vertx; public class Main { public static void main (String args[]) { System.out.println("Hello world"); // Create an HTTP server which simply returns "Hello World!" to each request. Vertx.vertx() .createHttpServer() .requestHandler(req -> { req.response() .putHeader("content-type", "text/plain") .end("Hello from Vert.x!"); }) .listen(8080, handler -> { if (handler.succeeded()) { System.out.println("create httpserver success"); } else { System.err.println("create httpserver fail"); } }); } }
这个程序只返回hello world,但是实际的情况中,程序会获取至少method(也就是本次请求属于POST请求还是GET请求),path(就是请求的url链接,包括get提交的问号后面的内容),以及body部分(post提交的参数)。
这节课就教教大家如何获取这几个参数。
method很容易得到,就是在回调中req参数的一个函数req.method()。path也很容易得到,就是req.path()即可。
body相对前两个来说,获取难度会比较大,因为 在实际http请求中,body可能会在连接很久后才得到,因此实际是在数据获取的回调函数中才能拿到。
回调的写法如下:
req.handler(body -> { // 接收到一个body请求 // 处理内容 }); req.endHandler(body -> { // 所有body请求接收完毕 // 处理内容 });
通常的处理方式是在req.handler中做字符拼接,在req.endHandler执行,如下:
Buffer datas = Buffer.buffer(); req.handler(data -> { datas.appendBuffer(data); }); req.endHandler(body -> { req.response() .putHeader("content-type", "text/plain") .putHeader("Access-Control-Allow-Origin", "*") .end("mothod:" + req.method() + " path:" + req.path() + " body:" + datas); });
此方法与nodejs中使用的方式很相近,完整的ava代码如下:
import io.vertx.core.Vertx; import io.vertx.core.buffer.Buffer; public class Main { public static void main (String args[]) { System.out.println("Hello world"); // Create an HTTP server which simply returns "Hello World!" to each request. Vertx.vertx() .createHttpServer() .requestHandler(req -> { Buffer datas = Buffer.buffer(); req.handler(data -> { datas.appendBuffer(data); }); req.endHandler(body -> { req.response() .putHeader("content-type", "text/plain") .putHeader("Access-Control-Allow-Origin", "*") .end("mothod:" + req.method() + " path:" + req.path() + " body:" + datas); }); req.bodyHandler(body -> { req.response() .putHeader("content-type", "text/plain") .putHeader("Access-Control-Allow-Origin", "*") .end("mothod:" + req.method() + " path:" + req.path() + " body:" + body); }); }) .listen(8080, handler -> { if (handler.succeeded()) { System.out.println("create httpserver success"); } else { System.err.println("create httpserver fail"); } }); } }
另外,vert.x还提供了另一个更加方便的接收body的工具,req.bodyHandler,他会在所有的body接收完毕后调用一次。调用方式如下:
req.bodyHandler(body -> { req.response() .putHeader("content-type", "text/plain") .putHeader("Access-Control-Allow-Origin", "*") .end("mothod:" + req.method() + " path:" + req.path() + " body:" + body); });
他可以减少代码量,完整的java文件如下:
import io.vertx.core.Vertx; public class Main { public static void main (String args[]) { System.out.println("Hello world"); // Create an HTTP server which simply returns "Hello World!" to each request. Vertx.vertx() .createHttpServer() .requestHandler(req -> { req.bodyHandler(body -> { req.response() .putHeader("content-type", "text/plain") .putHeader("Access-Control-Allow-Origin", "*") .end("mothod:" + req.method() + " path:" + req.path() + " body:" + body); }); }) .listen(8080, handler -> { if (handler.succeeded()) { System.out.println("create httpserver success"); } else { System.err.println("create httpserver fail"); } }); } }
文章作者:沃航科技