处理 Java Vert.x 上的 post 请求的有效方法?
Efficient way to handle post requests on Java Vert.x?
这就是我目前在 vert.x 服务器上处理 post 请求的方式:
router.post("/test").handler(context -> context.request().bodyHandler(body -> {
try {
JsonObject jsonObject = new JsonObject(body.toString());
...
} catch(Exception e) { }
}));
我正在使用 Postman 发送测试请求,其中主体的数据为 "raw - application/json"。
这行得通。但是,这是正确的方法吗?
我也尝试在 "form-data" 中将数据作为参数发送,但我无法获取参数。以下打印出整个请求,我可以看到数据,但无法将其解析为 json 或 map.
router.post("/test").handler(context ->
context.request().bodyHandler(System.out::println));
感谢任何帮助。谢谢。
您可以通过多种方式对请求处理程序进行编程。
您可以在本文档中找到不同的方法 https://vertx.io/docs/vertx-web/java/
这是我编写处理程序时更喜欢的方法。
package org.api.services.test;
import org.api.services.test.CustomDTO;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
public class TestApi extends AbstractVerticle {
@Override
public void start(Future<Void> startFuture) throws Exception {
super.start(startFuture);
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
//register a router for post request that accepts only requests with */json MIME type on exact path /test.
router.post("/test/").consumes("*/json").handler(this::testHandler);
...
}
private void testHandler(RoutingContext routingContext) {
//recommended way to extract json
JsonObject jsonObject = routingContext.getBodyAsJson();
//automatically map json to custom object
CustomDTO customDTO = Json.decodeValue(routingContext.getBodyAsString(), CustomDTO.class);
...
}
}
如果您要发送包含 form-data 的请求,您可以通过两种方式提取:
- 如果添加
router.route().handler(BodyHandler.create());
,那么所有表单属性都将合并为请求参数。
By default, the body handler will merge any form attributes into the request parameters. If you don’t want this behaviour you can use disable it with setMergeFormAttributes.
您可以使用 routingContext.request().getParam("attribute_name")
提取它们
- 如果您没有使用任何 BodyHandler,您需要设置
routingContext.request().setExpectMultipart(true);
,然后像这样访问表单属性 routingContext.request().formAttributes()
如果需要"form-data",请在句柄前加"BodyHandler"。
final Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
....
context.request().getParam("id")
这就是我目前在 vert.x 服务器上处理 post 请求的方式:
router.post("/test").handler(context -> context.request().bodyHandler(body -> {
try {
JsonObject jsonObject = new JsonObject(body.toString());
...
} catch(Exception e) { }
}));
我正在使用 Postman 发送测试请求,其中主体的数据为 "raw - application/json"。
这行得通。但是,这是正确的方法吗?
我也尝试在 "form-data" 中将数据作为参数发送,但我无法获取参数。以下打印出整个请求,我可以看到数据,但无法将其解析为 json 或 map.
router.post("/test").handler(context ->
context.request().bodyHandler(System.out::println));
感谢任何帮助。谢谢。
您可以通过多种方式对请求处理程序进行编程。 您可以在本文档中找到不同的方法 https://vertx.io/docs/vertx-web/java/
这是我编写处理程序时更喜欢的方法。
package org.api.services.test;
import org.api.services.test.CustomDTO;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Future;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.Router;
import io.vertx.ext.web.RoutingContext;
import io.vertx.ext.web.handler.BodyHandler;
public class TestApi extends AbstractVerticle {
@Override
public void start(Future<Void> startFuture) throws Exception {
super.start(startFuture);
Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
//register a router for post request that accepts only requests with */json MIME type on exact path /test.
router.post("/test/").consumes("*/json").handler(this::testHandler);
...
}
private void testHandler(RoutingContext routingContext) {
//recommended way to extract json
JsonObject jsonObject = routingContext.getBodyAsJson();
//automatically map json to custom object
CustomDTO customDTO = Json.decodeValue(routingContext.getBodyAsString(), CustomDTO.class);
...
}
}
如果您要发送包含 form-data 的请求,您可以通过两种方式提取:
- 如果添加
router.route().handler(BodyHandler.create());
,那么所有表单属性都将合并为请求参数。
By default, the body handler will merge any form attributes into the request parameters. If you don’t want this behaviour you can use disable it with setMergeFormAttributes.
您可以使用 routingContext.request().getParam("attribute_name")
- 如果您没有使用任何 BodyHandler,您需要设置
routingContext.request().setExpectMultipart(true);
,然后像这样访问表单属性routingContext.request().formAttributes()
如果需要"form-data",请在句柄前加"BodyHandler"。
final Router router = Router.router(vertx);
router.route().handler(BodyHandler.create());
....
context.request().getParam("id")