如何访问字符串请求?

时间:2018-09-08 23:27:02

标签: java vert.x

我在Java vert.x中有一个http服务器,并且从客户端收到一个带有字符串的请求,我需要将此字符串保存在变量中。如何访问发送的字符串?

public class Server extends AbstractVerticle {
    private Router router;
    private HttpServer httpServer = null;

    @Override
    public void start() throws Exception {
        httpServer = vertx.createHttpServer();
        httpServer.requestHandler(new Handler<HttpServerRequest>() {

            @Override
            public void handle(HttpServerRequest request) {
                System.out.println("incoming request!");
                Buffer fullRequestBody = Buffer.buffer();
                if (request.method() == HttpMethod.POST) {
                    request.handler(new Handler<Buffer>() {
                        @Override
                        public void handle(Buffer buffer) {
                            fullRequestBody.appendBuffer(buffer);
                        }
                    });
                    request.bodyHandler(new Handler<Buffer>() {
                        @Override
                        public void handle(Buffer buffer) {

                        }
                    });
                }
            }
        });

        httpServer.listen(9999);
    }

}

1 个答案:

答案 0 :(得分:1)

public class Server extends AbstractVerticle {
private Router router;
private HttpServer httpServer = null;

@Override
public void start() throws Exception {
    httpServer = vertx.createHttpServer();
    httpServer.requestHandler(new Handler<HttpServerRequest>() {

        @Override
        public void handle(HttpServerRequest request) {
            System.out.println("incoming request!");
            Buffer fullRequestBody = Buffer.buffer();
            if (request.method() == HttpMethod.POST) {
                request.handler(new Handler<Buffer>() {
                    @Override
                    public void handle(Buffer buffer) {
                        fullRequestBody.appendBuffer(buffer);
                    }
                });
                request.bodyHandler(new Handler<Buffer>() {
                    @Override
                    public void handle(Buffer buffer) {

                    }
                });
            }
        }
    });

    httpServer.listen(9999);
}

}