gpt4 book ai didi

java - 我怎样才能减少 vert.x 中的样板代码量

转载 作者:行者123 更新时间:2023-11-29 04:34:01 25 4
gpt4 key购买 nike

我读过几个关于 vertx.io 的教程但我仍然无法理解如何最大限度地减少重复代码。

例如,我需要实现从数据库获取数据的 RESTful 服务。我已经为表(客户、管理员)准备了 2 个 bean 类并实现了服务类:

AdministratorService.java:

public void getAll(RoutingContext routingContext) {
jdbc.getConnection(ar -> {
SQLConnection connection = ar.result();
connection.query(Queries.SELECT_ALL_ADMINS, result -> {
List<Administrator> admins = result.result().getRows().stream().map(Administrator::new).collect(Collectors.toList());
routingContext.response()
.putHeader("content-type", "application/json; charset=utf-8")
.end(Json.encodePrettily(admins));
connection.close();
});
});
}

public void getOneById(RoutingContext routingContext) {
final String id = routingContext.request().getParam("id");
if (id == null) {
routingContext.response().setStatusCode(400).end();
} else {
jdbc.getConnection(ar -> {
// Read the request's content and create an instance of Administrator.
SQLConnection connection = ar.result();
select(id, connection, Queries.SELECT_ONE_ADMIN_BY_ID, result -> {
if (result.succeeded()) {
routingContext.response()
.setStatusCode(200)
.putHeader("content-type", "application/json; charset=utf-8")
.end(Json.encodePrettily(result.result()));
} else {
routingContext.response()
.setStatusCode(404).end();
}
connection.close();
});
});
}
}

CustomerService.java:

public void getAll(RoutingContext routingContext) {
jdbc.getConnection(ar -> {
SQLConnection connection = ar.result();
connection.query(Queries.SELECT_ALL_CUSTOMERS, result -> {
List<Customer> customers = result.result().getRows().stream().map(Customer::new).collect(Collectors.toList());
routingContext.response()
.putHeader("content-type", "application/json; charset=utf-8")
.end(Json.encodePrettily(customers));
connection.close();
});
});
}


public void getOneById(RoutingContext routingContext) {
final String id = routingContext.request().getParam("id");
if (id == null) {
routingContext.response().setStatusCode(400).end();
} else {
jdbc.getConnection(ar -> {
// Read the request's content and create an instance of Administrator.
SQLConnection connection = ar.result();
select(id, connection, Queries.SELECT_ONE_CUSTOMER_BY_ID, result -> {
if (result.succeeded()) {
routingContext.response()
.setStatusCode(200)
.putHeader("content-type", "application/json; charset=utf-8")
.end(Json.encodePrettily(result.result()));
} else {
routingContext.response()
.setStatusCode(404).end();
}
connection.close();
});
});
}
}

不难看出那部分

.routingContext.response()
.putHeader("content-type", "application/json; charset=utf-8")

在每个方法中重复。一般来说,这些类之间的所有区别都是 sql 请求和 bean 类。

您能否分享您的示例或展示如何更改我的方法?

最佳答案

VertX 不是一个框架,这使得一些开发人员可以轻松设计自己的结构,但对于某些开发人员来说,它却成为噩梦。你正在寻找的是一个预先设计的框架,它已经准备好路由器、 Controller 、数据库连接。显然这不是 vertx 的意思,它更像是一个库,可以按照你想要的方式扩展它。

我在您的代码中看到,对于每个服务函数,您都获得了 SQL 连接。如果您使用过 Spring 等其他框架,则可以使用 DI 进行连接。您需要实现 DI、一些 MVC 设计,然后您的样板代码将被删除。

我做过类似的事情,但针对的是 MongoDB。

VertX with MongoDB

关于java - 我怎样才能减少 vert.x 中的样板代码量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42557301/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com