当前位置:网站首页>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
边栏推荐
- [cmake] cmake link SQLite Library
- PdfWriter. GetInstance throws system Nullreferenceexception [en] pdfwriter GetInstance throws System. NullRef
- GStreamer ffmpeg avdec decoded data flow analysis
- Chrome 98 Private Network Access problem w/ disabled web security: Request had no target IP address
- Realize the reuse of components with different routing parameters and monitor the changes of routing parameters
- 7.2 brush two questions
- LeetCode
- Interfaces and related concepts
- 【最詳細】最新最全Redis面試大全(50道)
- Advanced APL (realize group chat room)
猜你喜欢
691. 立方体IV
Le Seigneur des anneaux: l'anneau du pouvoir
Deep learning parameter initialization (I) Xavier initialization with code
[Fiddler problem] solve the problem about Fiddler's packet capturing. After the mobile network is configured with an agent, it cannot access the Internet
The embodiment of generics in inheritance and wildcards
为什么说数据服务化是下一代数据中台的方向?
Selenium key knowledge explanation
Common APIs
VMWare网络模式-桥接,Host-Only,NAT网络
Qtip2 solves the problem of too many texts
随机推荐
Map interface and method
带你全流程,全方位的了解属于测试的软件事故
SecureCRT取消Session记录的密码
FileInputStream and fileoutputstream
Custom generic structure
Summary of Arduino serial functions related to print read
Talk about floating
PAT甲级真题1166
万卷书 - 价值投资者指南 [The Education of a Value Investor]
3311. Longest arithmetic
JS monitors empty objects and empty references
Split small interface
Arduino Serial系列函数 有关print read 的总结
不出网上线CS的各种姿势
Spa single page application
The difference between typescript let and VaR
你开发数据API最快多长时间?我1分钟就足够了
Use of framework
II. D3.js draw a simple figure -- circle
Distributed lock