Skip to content

Instantly share code, notes, and snippets.

@downgoon
Created April 21, 2017 06:03
Show Gist options
  • Save downgoon/4ab169db1c98880d4ecb600c34382977 to your computer and use it in GitHub Desktop.
Save downgoon/4ab169db1c98880d4ecb600c34382977 to your computer and use it in GitHub Desktop.
Simple RESTful API using vertx-web
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServer;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
public class RestCrudHttpd {
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
HttpServer server = vertx.createHttpServer();
Router mainRouter = Router.router(vertx);
Router dbapiRouter = Router.router(vertx);
mainRouter.mountSubRouter("/dbapi", dbapiRouter);
dbapiRouter.route().handler(routingContext -> {
System.out.println("http request is comming ...: " + routingContext.request().absoluteURI());
routingContext.response().putHeader("Server", "autorest4db");
routingContext.response().putHeader("Content-Type", "application/json;charset=UTF-8");
routingContext.next();
});
dbapiRouter.route(HttpMethod.GET, "/:dbname").handler(routingContext -> {
String dbname = routingContext.request().getParam("dbname");
JsonObject json = new JsonObject().put("action", "list").put("dbname", dbname);
routingContext.response().end(json.toString());
});
dbapiRouter.route(HttpMethod.GET, "/:dbname/:id").handler(routingContext -> {
String dbname = routingContext.request().getParam("dbname");
String id = routingContext.request().getParam("id");
JsonObject json = new JsonObject().put("action", "detail").put("dbname", dbname).put("id", id);
routingContext.response().end(json.toString());
});
dbapiRouter.route(HttpMethod.POST, "/:dbname").handler(routingContext -> {
String dbname = routingContext.request().getParam("dbname");
JsonObject json = new JsonObject().put("action", "create").put("dbname", dbname);
routingContext.response().end(json.toString());
});
dbapiRouter.route(HttpMethod.PUT, "/:dbname/:id").handler(routingContext -> {
String dbname = routingContext.request().getParam("dbname");
String id = routingContext.request().getParam("id");
JsonObject json = new JsonObject().put("action", "update").put("dbname", dbname).put("id", id);
routingContext.response().end(json.toString());
});
dbapiRouter.route(HttpMethod.DELETE, "/:dbname/:id").handler(routingContext -> {
String dbname = routingContext.request().getParam("dbname");
String id = routingContext.request().getParam("id");
JsonObject json = new JsonObject().put("action", "delete").put("dbname", dbname).put("id", id);
routingContext.response().end(json.toString());
});
server.requestHandler(mainRouter::accept);
server.listen(8080);
System.out.println("listening on: 8080");
}
}
@downgoon
Copy link
Author

testing url

curl -i -X GET http://localhost:8080/dbapi/student
curl -i -X GET http://localhost:8080/dbapi/student/1234
curl -i -X POST http://localhost:8080/dbapi/student
curl -i -X PUT http://localhost:8080/dbapi/student/1234
curl -i -X DELETE http://localhost:8080/dbapi/student/1234

@downgoon
Copy link
Author

GET /student

$ curl -i -X GET http://localhost:8080/dbapi/student
HTTP/1.1 200 OK
Server: autorest4db
Content-Type: application/json;charset=UTF-8
Content-Length: 36

{"action":"list","dbname":"student"}%

@downgoon
Copy link
Author

GET /student/1234

$ curl -i -X GET http://localhost:8080/dbapi/student/1234
HTTP/1.1 200 OK
Server: autorest4db
Content-Type: application/json;charset=UTF-8
Content-Length: 50

{"action":"detail","dbname":"student","id":"1234"}

@downgoon
Copy link
Author

POST /student

$ curl -i -X POST http://localhost:8080/dbapi/student
HTTP/1.1 200 OK
Server: autorest4db
Content-Type: application/json;charset=UTF-8
Content-Length: 38

{"action":"create","dbname":"student"}

@downgoon
Copy link
Author

PUT /student/1234

$ curl -i -X PUT http://localhost:8080/dbapi/student/1234
HTTP/1.1 200 OK
Server: autorest4db
Content-Type: application/json;charset=UTF-8
Content-Length: 50

{"action":"update","dbname":"student","id":"1234"}

@downgoon
Copy link
Author

DELETE /student/1234

$ curl -i -X DELETE http://localhost:8080/dbapi/student/1234
HTTP/1.1 200 OK
Server: autorest4db
Content-Type: application/json;charset=UTF-8
Content-Length: 50

{"action":"delete","dbname":"student","id":"1234"}

@downgoon
Copy link
Author

downgoon commented Apr 21, 2017

maven dependency

<dependency>
      <groupId>io.vertx</groupId>
      <artifactId>vertx-web</artifactId>
      <version>3.4.1</version>
</dependency>

@downgoon
Copy link
Author

重构-1

DbapiAction

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment