package fr.imta.smartgrid;
import io.vertx.core.Handler;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.RoutingContext;
public class DisplayRequestHandler3 implements Handler<RoutingContext> {
@Override
public void handle(RoutingContext event) {
JsonObject res = new JsonObject();
res.put("msg","Hello world!");
if (event.pathParams().isEmpty())
res.put("name","anonymous");
else res.put("name",event.pathParam("name"));
event.json(res);
}
}
package fr.imta.smartgrid;
import io.vertx.core.Vertx;
import io.vertx.core.datagram.DatagramSocket;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.handler.BodyHandler;
public class VertxServer {
private Vertx vertx;
public VertxServer() {
// initialize the Vertx server we will be using later
this.vertx = Vertx.vertx();
}
public void start() {
// create a router that we will use to route HTTP requests to different handlers
Router router = Router.router(vertx);
// register a handler that process payload of HTTP requests for us
router.route().handler(BodyHandler.create());
// we register handlers here
DisplayRequestHandler3 myHandler = new DisplayRequestHandler3();
// this will catch all call to localhost:8080/hello whatever the HTTP method is
router.route("/hello").handler(myHandler);
// if you do not plan to reuse the same handler for multiple routes you can write
router.route("/hello").handler(new DisplayRequestHandler3());
// this will catch GET / POST requests to localhost:8080/hello/<something>
router.get("/hello/:name").handler(myHandler);
router.get("/hello/:prenom/:nom").handler(new DisplayRequestHandler2());
router.post("/hello/:name").handler(myHandler);
// we start the HTTP server
vertx.createHttpServer().requestHandler(router).listen(8080);
// create a UDP socket
DatagramSocket socket = vertx.createDatagramSocket();
// register a handler for this server
socket.handler(new UDPHandler());
// start the server to listen on all interfaces on port 12345
socket.listen(12345, "0.0.0.0");
System.out.println("Server started");
}
public static void main(String[] args) {
new VertxServer().start();
}
}