Enterprise Java

Setting basic response http headers for REST resources on a simple Vertx Rest based app

I am new to Vert.x but as a Java developer (die hard) I find it much more enjoyable and promising comparing to NodeJS or whatever – Reactor based frameworks/libraries. So I was going through implementing a very simple Restful API, using Vert.x.

My problem for today is that I wanted to include some certain HttpHeaders in most (all) of my , responses. For example set the Content-type to “application/json”. In the future maybe add some others.

I was kind of wondering around as a Vert.x newbie, then I kind of realized that what is was eventually suggested in this blog post (See the use of BodyHandler) would actually work for me.

So I had my main VertxMain java app where I register my MyWebVerticleApp.

package com.javapapo.vertxweb;

import io.vertx.core.Vertx;
import io.vertx.core.VertxOptions;

/**
 * Created by <a href="mailto:javapapo@mac.com">javapapo</a> on 15/11/15.
 */
public class VertxEngineMain {
    public static void main(String[] args) {
        VertxOptions opts = new VertxOptions();
        Vertx vertx = Vertx.vertx(opts);
        vertx.deployVerticle(new MyWebVerticleApp());
    }



}

Then I have created a small handler, I call it BaseResponseHandler, that eventually adds the HttpHeader in my responses.

package com.javapapo.vertxweb.handlers;

import io.netty.handler.codec.http.HttpResponse;
import io.vertx.core.Handler;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.RoutingContext;

/**
 * Created by <a href="mailto:javapapo@mac.com">javapapo</a> on 27/11/15.
 */
public class BaseResponseHandler implements Handler<RoutingContext>{

    @Override
    public void handle(RoutingContext context) {
        HttpServerResponse response = context.response();
        response.putHeader(HttpHeaders.CONTENT_TYPE.toString(), "application/json");
        //other stuff!
        response.setChunked(true);
        context.next();
    }

}

Then in my MyWebVerticle I just register the handler to be invoked all the time, in the router chaining.

package com.javapapo.vertxweb;

import com.javapapo.vertxweb.handlers.BaseResponseHandler;
import com.javapapo.vertxweb.handlers.StatusHandler;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.ext.web.Route;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;

/**
 * Created by <a href="mailto:javapapo@mac.com">javapapo</a> on 16/11/15.
 */
public class MyWebVerticleApp extends AbstractVerticle {
    @Override
    public void start(Future<Void> fut) {
        HttpServer server = vertx.createHttpServer();
        Router router = Router.router(vertx);
        //enable the base response handler overall!
        router.route().handler(new BaseResponseHandler());
        router.route("/status/").handler(new StatusHandler());
        server.requestHandler(router::accept).listen(8080);
    }
}

Paris Apostolopoulos

Paris is a senior software engineer focusing on J2EE development, loves Business process modelling and is keen on software quality challenges. He is passionate about Java and Java communities. He is a co-founder and administrator of the first Java User Group in greece(JHUG.gr) and occasional speaker on meet-ups and seminars and regular blogger. For his contributions and involvement on the Java community he has been awarded the title of Java Champion in 2007 by Sun Microsystems.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button