Java Vert.x-从POST请求获取参数
我正在尝试创建一个Vert.x Rest服务,该服务响应某些URL\analysis上的POST请求 使用以下命令 curl-D-\analyze-D'{“text”:“bla”} 我想从命令中提取“bla”,并对其执行简单的文本分析: 以下是我的代码草案:Java Vert.x-从POST请求获取参数,java,rest,post,vert.x,Java,Rest,Post,Vert.x,我正在尝试创建一个Vert.x Rest服务,该服务响应某些URL\analysis上的POST请求 使用以下命令 curl-D-\analyze-D'{“text”:“bla”} 我想从命令中提取“bla”,并对其执行简单的文本分析: 以下是我的代码草案: @Override public void start(Future<Void> fut) throws Exception { router = Router.router(vertx); route
@Override
public void start(Future<Void> fut) throws Exception {
router = Router.router(vertx);
router.post("/analyze").handler(this::analyze);
// Create Http server and pass the 'accept' method to the request handler
vertx.createHttpServer().requestHandler(router::accept).
listen(config().getInteger("http.port", 9000),
result -> {
if (result.succeeded()) {
System.out.println("Http server completed..");
fut.complete();
} else {
fut.fail(result.cause());
System.out.println("Http server failed..");
}
}
);
}
private void analyze(RoutingContext context) {
HttpServerResponse response = context.response();
String bodyAsString = context.getBodyAsString();
JsonObject body = context.getBodyAsJson();
if (body == null){
response.end("The Json body is null. Please recheck.." + System.lineSeparator());
}
else
{
String postedText = body.getString("text");
response.setStatusCode(200);
response.putHeader("content-type", "text/html");
response.end("you posted json which contains the following " + postedText);
}
}
@覆盖
public void start(Future fut)引发异常{
路由器=路由器。路由器(vertx);
router.post(“/analyze”).handler(this::analyze);
//创建Http服务器并将“accept”方法传递给请求处理程序
createHttpServer().requestHandler(路由器::接受)。
侦听(config().getInteger(“http.port”,9000),
结果->{
if(result.successed()){
System.out.println(“Http服务器已完成…”);
fut.complete();
}否则{
进一步失败(result.cause());
System.out.println(“Http服务器失败…”);
}
}
);
}
专用void分析(RoutingContext上下文){
HttpServerResponse=context.response();
字符串bodyAsString=context.getBodyAsString();
JsonObject body=context.getBodyAsJson();
if(body==null){
response.end(“Json正文为空。请重新检查..”+System.lineSeparator());
}
其他的
{
String postedText=body.getString(“文本”);
响应。设置状态代码(200);
响应.putHeader(“内容类型”、“文本/html”);
end(“您发布了包含以下内容的json”+postedText);
}
}
}
你知道我怎样才能从邮局拿到“bla”吗 尝试以下路由器和处理程序:
Router router = Router.router(vertx);
// add a handler which sets the request body on the RoutingContext.
router.route().handler(BodyHandler.create());
// expose a POST method endpoint on the URI: /analyze
router.post("/analyze").handler(this::analyze);
// handle anything POSTed to /analyze
public void analyze(RoutingContext context) {
// the POSTed content is available in context.getBodyAsJson()
JsonObject body = context.getBodyAsJson();
// a JsonObject wraps a map and it exposes type-aware getters
String postedText = body.getString("text");
context.response().end("You POSTed JSON which contains a text attribute with the value: " + postedText);
}
使用上面的代码执行此CURL命令
curl -D- http://localhost:9000/analyze -d '{"text":"bla"}'
。。。将返回:
$ curl -D- http://localhost:9000/analyze -d '{"text":"bla"}'
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 67
Set-Cookie: vertx-web.session=21ff020c9afa5ec9fd5948acf64c5a85; Path=/
You POSTed JSON which contains a text attribute with the value: bla
查看您的问题,您已经定义了一个名为/analyze
的端点,但随后建议使用以下CURL命令:CURL-D-http://localhost:8080 -d'{“text”:“bla”}
它不与/analyze
端点对话。也许这是问题的一部分,也许这只是准备问题时的一个输入错误。无论如何,我上面提供的代码将:
- 在
http://localhost:9000/analyze
- 处理发布到该端点的内容
router.route().handler(BodyHandler.create())代码>这为您的Web服务器提供了一个处理程序,该处理程序在RoutingContext上设置请求正文,以便您可以使用context.getBodyXXX()