Created
April 21, 2017 06:03
-
-
Save downgoon/4ab169db1c98880d4ecb600c34382977 to your computer and use it in GitHub Desktop.
Simple RESTful API using vertx-web
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"); | |
| } | |
| } |
Author
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"}%
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"}
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"}
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"}
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"}
Author
maven dependency
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
<version>3.4.1</version>
</dependency>
Author
重构-1
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
testing url