当前位置:网站首页>Vertx restful style web router
Vertx restful style web router
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 , Support Restful style url Definition
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
@Slf4j
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();
log.info("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));
return mainRouter;
}
}
public class UserRoute {
public Router create(Vertx vertx) {
Router userRouter = Router.router(vertx);
UserService service = new UserService();
userRouter.get("/list").handler(service::list);
userRouter.post("/info").handler(service::add);
userRouter.put("/info").handler(service::update);
userRouter.delete("/info/:userId").handler(service::delete);
userRouter.get("/info/:userId").handler(service::detail);
return userRouter;
}
}
@Slf4j
public class UserService {
private final static List<UserVo> USERS = new ArrayList<>();
static {
USERS.add(UserVo.of(1L, "Jim Green", "[email protected]", 21));
USERS.add(UserVo.of(2L, "Tom Dio", "[email protected]", 22));
USERS.add(UserVo.of(3L, "Mrs Mei", "[email protected]", 23));
USERS.add(UserVo.of(4L, "Tom Holland", "[email protected]", 24));
USERS.add(UserVo.of(5L, "Zendaya", "[email protected]", 25));
}
public void list(RoutingContext ctx) {
log.info("===> get all users");
R<List<UserVo>> result = R.data(USERS);
ctx.json(result);
}
public void detail(RoutingContext ctx) {
String userId = ctx.pathParam("userId");
log.info("===> detail user: {}", userId);
UserVo userVo = USERS.stream().filter(u -> u.getUserId().toString().equals(userId)).findFirst().orElse(null);
ctx.json(R.data(userVo));
}
public void add(RoutingContext ctx) {
UserVo userVo = ctx.getBodyAsJson().mapTo(UserVo.class);
log.info("===> save user: {}", userVo);
USERS.add(userVo);
ctx.json(R.success("save"));
}
public void update(RoutingContext ctx) {
UserVo userVo = ctx.getBodyAsJson().mapTo(UserVo.class);
log.info("===> update user: {}", userVo);
ctx.json(R.success("update"));
}
public void delete(RoutingContext ctx) {
String userId = ctx.pathParam("userId");
log.info("===> delete user: {}", userId);
ctx.json(R.success("delete"));
}
}
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
Vertx And Router Restful route Github Address
Vertx And Router Restfull route Gitee Address
边栏推荐
猜你喜欢
![[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*](/img/1f/f579110a408c5b5a094733be57ed90.jpg)
[set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*

Understanding of class

691. 立方体IV

VMWare网络模式-桥接,Host-Only,NAT网络

Pat grade a real problem 1166

高并发内存池

【开发笔记】基于机智云4G转接板GC211的设备上云APP控制
![[set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)](/img/d8/b4f39d9637c9886a8c81ca125d6944.jpg)
[set theory] Stirling subset number (Stirling subset number concept | ball model | Stirling subset number recurrence formula | binary relationship refinement relationship of division)

VMware network mode - bridge, host only, NAT network

Use of generics
随机推荐
Lombok -- simplify code
PgSQL converts string to double type (to_number())
论文学习——鄱阳湖星子站水位时间序列相似度研究
sharepoint 2007 versions
Common architectures of IO streams
Advanced API (character stream & net for beginners)
Circuit, packet and message exchange
Various postures of CS without online line
Topic | synchronous asynchronous
Discussion on some problems of array
【已解决】win10找不到本地组策略编辑器解决方法
SecureCRT取消Session记录的密码
MySQL syntax (basic)
gstreamer ffmpeg avdec解码数据流向分析
7.2 brush two questions
Common methods of file class
Margin left: -100% understanding in the Grail layout
The difference between typescript let and VaR
Distributed lock
Jeecg data button permission settings