Juvenik
Juvenik

Reputation: 950

Generate OpenAPI spec for vertx project

I want to generate an OpenAPI spec for my vertx project. So I have a simple vertx server as follows which just returns me a json object:

package server;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.http.HttpServer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;

public class Server extends AbstractVerticle {

  @Override
  public void start() throws Exception {
    HttpServer server = vertx.createHttpServer();
    Router router = Router.router(vertx);
    router.route("/v0.2.2/*").handler(this::responseSetUp);
    router.get("/v0.2.2/location").handler(this::getLocation);
    server.requestHandler(router::accept).listen(8004);
  }

  public void responseSetUp(RoutingContext context) {
    HttpServerResponse response = context.response();
    response.putHeader("Access-Control-Allow-Origin", "*")
        .putHeader("Access-Control-Allow-Methods", "GET, POST, PUT , DELETE, OPTIONS")
        .putHeader("Access-Control-Allow-Headers", "Content-Type,cache-control, x-requested-with")
        .putHeader("Access-Control-Max-Age", "86400");
    context.next();
  }

  public void getLocation(RoutingContext context) {
    JsonObject location = new JsonObject();
    location.put("city", "Bangalore");
    location.put("country", "India");
    location.put("pin", 560095);
    HttpServerResponse response = context.response();
    response.putHeader("content-type", "application/json");
    response.setChunked(true);
    response.write(location.toString());
    response.end();
  }
}

I just want to generate an OpenAPI spec for this route. I am new to Swagger and so far while googling, I could see tutorials for RestEasy, Mule, Jersey1,Jersey2, Spring but not for vertx.

Can anyone please help me out or any pointers?

Sorry, if the question is naive.

Upvotes: 8

Views: 1872

Answers (1)

Francesco Guardiani
Francesco Guardiani

Reputation: 688

With Vert.x 3.5.0 there's a new package called Vert.x Web API Contract that helps you generate the Router and the Validation handlers for your request. You can also generate a skeleton of your project with slush-vertx, a command line tool that helps you scaffolding your project

Upvotes: 4

Related Questions