user4522478
user4522478

Reputation:

How to make Vertx request handler expect JSON data?

I am running a VertX HTTP Server. It understands requests when content type is HTML/forms, but when I try to post JSON data, it never even enters the request handler.

Is there something I need to do to make Vertx expect JSON? Is this supported?

Upvotes: 1

Views: 4543

Answers (2)

Agraj
Agraj

Reputation: 474

If you can, Check out the latest Vertx-web They provide a really neat way of handling various requests formats ( multipart-data , url-encoded params, file uploads etc ) using elegant syntax ala NodeJs routing

Though you would need to migrate your code to Vertx 3.0

Upvotes: 1

Thomas
Thomas

Reputation: 4330

Here is a java example. Note that the data handler which will be processing json is executed only for post request. Post a request with some json data to this and it will return with the same.

import org.vertx.java.core.Handler;
import org.vertx.java.core.buffer.Buffer;
import org.vertx.java.core.http.HttpServerRequest;
import org.vertx.java.platform.Verticle;

/**
 * Simple Http server
 */
public class HttpVerticle extends Verticle {
    public void start() {
        vertx.createHttpServer()
                .requestHandler(new Handler<HttpServerRequest>() {
                    @Override
                    public void handle(final HttpServerRequest request) {
                        container.logger().info(
                                "Got request for " + request.path());
                        if (request.method().equalsIgnoreCase("POST")) {
                            request.dataHandler(new Handler<Buffer>() {
                                @Override
                                public void handle(Buffer data) {
                                    request.response().end("got data " + data);
                                }
                            });
                        } else {
                            request.response().end("got request");
                        }
                    }
                }).listen(8080);
        container.logger().info("HttpVerticle started");
    }
}

Upvotes: 3

Related Questions