Java 当我试图使用Vertex HTTP客户端获取数据时,它会给我null

Java 当我试图使用Vertex HTTP客户端获取数据时,它会给我null,java,vert.x,vertx-httpclient,Java,Vert.x,Vertx Httpclient,嗨,任何人都可以看看: 当我尝试使用VERTX HTTP客户端获取数据时,它在最后一行中给我空指针异常,并且当我使用java HTTP客户端时,相同的URL提供数据: HttpClientOptions options = new HttpClientOptions().setKeepAlive(false); options.setLogActivity(true); options.setDefaultHost("http://delvmpllbbab10"); options.s

嗨,任何人都可以看看:

当我尝试使用VERTX HTTP客户端获取数据时,它在最后一行中给我空指针异常,并且当我使用java HTTP客户端时,相同的URL提供数据:

HttpClientOptions options = new HttpClientOptions().setKeepAlive(false);

options.setLogActivity(true);    
options.setDefaultHost("http://delvmpllbbab10");
options.setDefaultPort(7003);

HttpClient client = vertx.createHttpClient(options);
HttpClientResponse[] clientResponse = {null};

client.request(HttpMethod.GET, "/rcsservices/homePage", response -> {
    System.out.println("Received response with status code " + response.statusCode());

    clientResponse[0] = response;

}).putHeader("content-type", "application/json").end(clientResponse[0].toString());

这是代码吗我有一些问题…

这是正确的,您将得到一个
NPE
,因此在代码中,您使用
{null}
初始化
clientResponse
变量。如果您现在遵循代码的执行方式,您将看到原因:

  • 创建请求对象:
    client.request(
  • 您可以配置接收到响应后将执行的内容:`response->{…}
  • 您向请求添加一个头:
    putHeader(…)
  • 您触发请求
    end(clientResponse[0].toString())
  • 现在,您看到
    clientResponse[0]
    仍然为空(这是您初始化它的值。因此发生的情况是您正在调用:

    null.toString()
    

    这是无效的,并且总是抛出一个
    Null指针异常

    如果正确,您将得到一个
    NPE
    ,因此在您的代码中,您使用
    {Null}
    初始化
    clientResponse
    变量。如果您现在遵循代码的执行方式,您将看到原因:

  • 创建请求对象:
    client.request(
  • 您可以配置接收到响应后将执行的内容:`response->{…}
  • 您向请求添加一个头:
    putHeader(…)
  • 您触发请求
    end(clientResponse[0].toString())
  • 现在,您看到
    clientResponse[0]
    仍然为空(这是您初始化它的值。因此发生的情况是您正在调用:

    null.toString()
    

    这是无效的,将始终引发
    空指针异常

    我在开始使用Vert-X framework时遇到了相同的问题,我可以从您的代码中注意到以下改进:

  • 您必须使用HttpClientResponse的bodyHandler处理响应,然后您不能输入外部变量,除非是final
  • 为了不阻塞调用线程,最好使用asyncResponse,特别是当您依赖外部系统(在您的情况下是另一个Web服务)时
  • 我不确定您是否要提供正文,请求处的
    .end()
    包含新的“客户机请求正文”,但在生成GET时,正文必须为空
  • 尝试使用标准的
    HttpHeaders
    MediaType
    (我个人使用JAX-RS中的
    MediaType
    ),而不是编写自己的字符串
  • 尝试使用合适的记录器,而不是使用System.out.println(…)
  • 以下代码显示了如何将响应返回给原始调用方:

    public void callWebService(@Context HttpServerRequest request, @Suspended final AsyncResponse asyncResponse) {
    
        HttpClientOptions options = new HttpClientOptions().setKeepAlive(false);
    
        options.setLogActivity(true);    
        options.setDefaultHost("http://delvmpllbbab10");
        options.setDefaultPort(7003);
    
        HttpClient client = vertx.createHttpClient(options);
    
        client.request(HttpMethod.GET, "/rcsservices/homePage", response -> {
    
            System.out.println("Received response with status code " + response.statusCode());
            int code = response.statusCode();
            if (code == 200) {
                response.bodyHandler(bufferResponse -> {
                    // Adapt according your response type, could be String as well
                    JsonObject httpResult = bufferResponse.toJsonObject();
                    System.out.println("Received HTTP response with body " + httpResult);
                    asyncResponse.resume(Response.ok(httpResult).build());
                });
            } else {
    
                response.bodyHandler(bufferResponse -> {
                    // Return null in a JSON Object in case of error
                    String httpResult = "{null}";
                    asyncResponse.resume(Response.status(code).entity(httpResult).build());
                });
            }
    
    }).putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).end();
    

    有很多方法可以做同样的事情,您可以查看。

    我在开始使用Vert-X framework时遇到了相同的问题,我可以从您的代码中注意到以下改进:

  • 您必须使用HttpClientResponse的bodyHandler处理响应,然后您不能输入外部变量,除非是final
  • 为了不阻塞调用线程,最好使用asyncResponse,特别是当您依赖外部系统(在您的情况下是另一个Web服务)时
  • 我不确定您是否要提供正文,请求处的
    .end()
    包含新的“客户机请求正文”,但在生成GET时,正文必须为空
  • 尝试使用标准的
    HttpHeaders
    MediaType
    (我个人使用JAX-RS中的
    MediaType
    ),而不是编写自己的字符串
  • 尝试使用合适的记录器,而不是使用System.out.println(…)
  • 以下代码显示了如何将响应返回给原始调用方:

    public void callWebService(@Context HttpServerRequest request, @Suspended final AsyncResponse asyncResponse) {
    
        HttpClientOptions options = new HttpClientOptions().setKeepAlive(false);
    
        options.setLogActivity(true);    
        options.setDefaultHost("http://delvmpllbbab10");
        options.setDefaultPort(7003);
    
        HttpClient client = vertx.createHttpClient(options);
    
        client.request(HttpMethod.GET, "/rcsservices/homePage", response -> {
    
            System.out.println("Received response with status code " + response.statusCode());
            int code = response.statusCode();
            if (code == 200) {
                response.bodyHandler(bufferResponse -> {
                    // Adapt according your response type, could be String as well
                    JsonObject httpResult = bufferResponse.toJsonObject();
                    System.out.println("Received HTTP response with body " + httpResult);
                    asyncResponse.resume(Response.ok(httpResult).build());
                });
            } else {
    
                response.bodyHandler(bufferResponse -> {
                    // Return null in a JSON Object in case of error
                    String httpResult = "{null}";
                    asyncResponse.resume(Response.status(code).entity(httpResult).build());
                });
            }
    
    }).putHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).end();
    
    有很多方法可以做同样的事情,你可以检查