【问题标题】:Java Akka actor's HTTP POST request not hitting the post pathJava Akka 演员的 HTTP POST 请求未命中发布路径
【发布时间】:2021-11-12 02:49:50
【问题描述】:

我正在使用 akka 版本 2.6.17 和 akka http 10.2.7 创建一个发布请求。我的系统绑定到 8080 端口,可以很好地接收来自 Postman 的 post 请求。但是,当从 akka 本身(发送 post 请求的参与者)内部发送 post 请求时,POST 路径永远不会被命中。 这是发帖请求

public void postStockCheck(){
        String data = "{\"id\": \"yes\"}";
        HttpRequest.POST("http://localhost:8080/hello")
        .withEntity(HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8, data));
    }

这是路径:

return route(
        path("hello", () ->
            post(() ->
                entity(Unmarshaller.entityToString(), (string) -> {
                  System.out.println("request body: " + string);
                  return complete("Success");
                })
              )));
      }

如前所述,该路径将从邮递员处工作。如果我遗漏了什么,我们将不胜感激!

【问题讨论】:

    标签: java post akka akka-http


    【解决方案1】:

    postStockCheck 中,您创建了一个HttpRequest,但还没有解雇它。要触发请求,您可以使用来自HttpsingleRequest 方法。

    public void postStockCheck(ActorSystem system) {
        String data = "{\"id\": \"yes\"}";
    
        Http.get(system)
                .singleRequest(HttpRequest.POST("http://localhost:8080/hello")
                .withEntity(HttpEntities.create(ContentTypes.TEXT_PLAIN_UTF8, data)));
    }
    

    要更好地了解触发请求和收集响应,请转至docs

    【讨论】:

      猜你喜欢
      • 2017-10-21
      • 2015-07-27
      • 2021-05-25
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多