我使用Vert.x框架服务器端和Angular 2(v6.0.9)客户端创建了一个简单的REST应用程序。我要做的是使服务器显示接收到的数据。但我无法弄清楚如何检索HTTP请求正文:既routingContext.getBodyAsString()与routingContext.getBodyAsJson()返回null。作为一个临时解决方案,我设法使用pathgetParam("data")方法通过path参数显示了所有已发送的数据。我究竟做错了什么?服务器代码package test.post;import java.util.HashSet;import java.util.Set;import io.vertx.core.AbstractVerticle;import io.vertx.core.Vertx;import io.vertx.core.http.HttpMethod;import io.vertx.core.json.Json;import io.vertx.ext.web.Router;import io.vertx.ext.web.RoutingContext;import io.vertx.ext.web.handler.CorsHandler;import io.vertx.ext.web.handler.StaticHandler;public class Server extends AbstractVerticle { @Override public void start() throws Exception { Router router = Router.router(vertx); Set<String> allowedHeaders = new HashSet<>(); allowedHeaders.add("x-requested-with"); allowedHeaders.add("Access-Control-Allow-Origin"); allowedHeaders.add("origin"); allowedHeaders.add("Content-Type"); allowedHeaders.add("accept"); allowedHeaders.add("X-PINGARUNER"); Set<HttpMethod> allowedMethods = new HashSet<>(); allowedMethods.add(HttpMethod.GET); allowedMethods.add(HttpMethod.POST); allowedMethods.add(HttpMethod.OPTIONS); allowedMethods.add(HttpMethod.DELETE); allowedMethods.add(HttpMethod.PATCH); allowedMethods.add(HttpMethod.PUT); router.route().handler(CorsHandler.create("*").allowedHeaders(allowedHeaders).allowedMethods(allowedMethods)); router.post("/test/post/handler/:data").handler(this::receive); // Serve the static pages router.route().handler(StaticHandler.create()); vertx.createHttpServer().requestHandler(router::accept).listen(8080); System.out.println("Service running"); }
添加回答
举报
0/150
提交
取消