当前位置:网站首页>Web router of vertx
Web router of vertx
2022-07-03 07:25:00 【Sleeping Empire】
Introduce
Router from HttpServer Receive the request and route it to the first matching route it contains . A router can contain many sub routes .
1. maven Project dependence
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>com.lance.common</groupId>
<artifactId>vertx-common-core</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
2. Project part code
public class MainApp extends AbstractVerticle {
@Override
public void start(Promise<Void> startPromise) throws Exception {
HttpServer server = vertx.createHttpServer();
server.requestHandler(new MainRoute().create(vertx));
server.listen(8888, http -> {
if (http.succeeded()) {
startPromise.complete();
System.out.println("HTTP server started on port 8888");
} else {
startPromise.fail(http.cause());
}
});
}
}
public class MainRoute {
public Router create(Vertx vertx) {
Router mainRouter = Router.router(vertx);
mainRouter.route().consumes("application/json; charset=utf-8");
mainRouter.route().produces("application/json; charset=utf-8");
mainRouter.route().handler(BodyHandler.create());
mainRouter.mountSubRouter("/user", new UserRoute().create(vertx));
mainRouter.mountSubRouter("/book", new BookRoute().create(vertx));
return mainRouter;
}
}
public class UserRoute {
private final static Logger log = LoggerFactory.getLogger(UserRoute.class);
public Router create(Vertx vertx) {
log.info("=====>Init user router.");
Router userRouter = Router.router(vertx);
UserService service = new UserService();
userRouter.get("/list").handler(service::list);
userRouter.post("/add").handler(service::add);
return userRouter;
}
}
@Slf4j
public class UserService {
private final static List<UserVo> USERS = new ArrayList<>();
static {
USERS.add(UserVo.of(1L, "Jim Green", "[email protected]", 20));
USERS.add(UserVo.of(2L, "Tom Dio", "[email protected]", 21));
USERS.add(UserVo.of(3L, "Mrs Mei", "[email protected]", 22));
USERS.add(UserVo.of(4L, "Tom Holland", "[email protected]", 23));
USERS.add(UserVo.of(5L, "Zendaya", "[email protected]", 24));
}
public void list(RoutingContext ctx) {
log.info("===> get all users");
R<List<UserVo>> result = R.data(USERS);
ctx.json(result);
}
public void add(RoutingContext ctx) {
log.info("===> save user info");
ctx.json(R.success("ok"));
}
}
3. Project test
After the project starts , http://127.0.0.1:8888/user/list
{
"code": "200",
"success": true,
"data": [
{
"userId": 1,
"username": "Jim Green",
"password": "[email protected]",
"age": 20
},
{
"userId": 2,
"username": "Tom Dio",
"password": "[email protected]",
"age": 21
}
],
"msg": null
}
4. Full address of the project
边栏推荐
猜你喜欢
SecureCRT password to cancel session recording
3311. 最长算术
[HCAI] learning summary OSI model
Hash table, generic
“百度杯”CTF比赛 2017 二月场,Web:爆破-1
Homology policy / cross domain and cross domain solutions /web security attacks CSRF and XSS
为什么说数据服务化是下一代数据中台的方向?
URL programming
Margin left: -100% understanding in the Grail layout
"Baidu Cup" CTF game 2017 February, Web: blast-1
随机推荐
在 4EVERLAND 上存储 WordPress 媒体内容,完成去中心化存储
Visit Google homepage to display this page, which cannot be displayed
Hisat2 - stringtie - deseq2 pipeline for bulk RNA seq
Distributed lock
SQL create temporary table
HISAT2 - StringTie - DESeq2 pipeline 进行bulk RNA-seq
JUC forkjoinpool branch merge framework - work theft
【已解决】win10找不到本地组策略编辑器解决方法
Introduction of buffer flow
[solved] unknown error 1146
《指環王:力量之戒》新劇照 力量之戒鑄造者亮相
TCP cumulative acknowledgement and window value update
论文学习——鄱阳湖星子站水位时间序列相似度研究
Some experiences of Arduino soft serial port communication
Flask Foundation
深度学习参数初始化(一)Xavier初始化 含代码
Arduino Serial系列函数 有关print read 的总结
[set theory] partition (partition | partition example | partition and equivalence relationship)
1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
Common architectures of IO streams