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); } }
Reference: | Setting basic response http headers for REST resources on a simple Vertx Rest based app from our JCG partner Paris Apostolopoulos at the Papo’s log blog. |