当前位置:网站首页>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
边栏推荐
- JUC forkjoinpool branch merge framework - work theft
- How long is the fastest time you can develop data API? One minute is enough for me
- 4EVERLAND:IPFS 上的 Web3 开发者中心,部署了超过 30,000 个 Dapp!
- PgSQL converts string to double type (to_number())
- 4279. Cartesian tree
- 万卷书 - 价值投资者指南 [The Education of a Value Investor]
- [set theory] equivalence classes (concept of equivalence classes | examples of equivalence classes | properties of equivalence classes | quotient sets | examples of quotient sets)*
- c语言指针的概念
- Docker builds MySQL: the specified path of version 5.7 cannot be mounted.
- Distributed lock
猜你喜欢

Final, override, polymorphism, abstraction, interface

691. Cube IV

4279. Cartesian tree

Use of other streams

Leetcode 213: 打家劫舍 II

Take you through the whole process and comprehensively understand the software accidents that belong to testing

Margin left: -100% understanding in the Grail layout

C code production YUV420 planar format file

Arduino Serial系列函数 有关print read 的总结

File operation serialization recursive copy
随机推荐
1. E-commerce tool cefsharp autojs MySQL Alibaba cloud react C RPA automated script, open source log
4everland: the Web3 Developer Center on IPFs has deployed more than 30000 dapps!
Leetcode 198: 打家劫舍
Operation and maintenance technical support personnel have hardware maintenance experience in Hong Kong
Specified interval inversion in the linked list
Strategy mode
Thoughts on project development
FileInputStream and fileoutputstream
Warehouse database fields_ Summary of SQL problems in kingbase8 migration of Jincang database
Jeecg data button permission settings
3311. Longest arithmetic
The underlying mechanism of advertising on websites
[solved] sqlexception: invalid value for getint() - 'Tian Peng‘
Book recommendation~
《指环王:力量之戒》新剧照 力量之戒铸造者亮相
Use of framework
New stills of Lord of the rings: the ring of strength: the caster of the ring of strength appears
10 000 volumes - Guide de l'investisseur en valeur [l'éducation d'un investisseur en valeur]
IO stream system and FileReader, filewriter
Common methods of file class