Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 242
Juvenik Avatar asked Dec 07 '17 06:12

Juvenik


People also ask

What is an OpenAPI generator?

OpenAPI Generator is a tool designed to create API client libraries, server stubs, configurations, and documentation from OpenAPI 2.0 and 3. x documents. It boasts a wide range of functions and is used by a wide range of users, some of whom are also maintainers.

Is YAML OpenAPI?

OpenAPI allows for either a JSON or YAML format. We recommend using the YAML format, and use it in our examples.

What is the OpenAPI Specification format?

OpenAPI Specification (formerly Swagger Specification) is an API description format for REST APIs. An OpenAPI file allows you to describe your entire API, including: Available endpoints ( /users ) and operations on each endpoint ( GET /users , POST /users ) Operation parameters Input and output for each operation.


1 Answers

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

like image 169
Francesco Guardiani Avatar answered Oct 11 '22 04:10

Francesco Guardiani