Java 当我试图使用Vertex HTTP客户端获取数据时,它会给我null
嗨,任何人都可以看看: 当我尝试使用VERTX HTTP客户端获取数据时,它在最后一行中给我空指针异常,并且当我使用java HTTP客户端时,相同的URL提供数据: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
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(
putHeader(…)
end(clientResponse[0].toString())
clientResponse[0]
仍然为空(这是您初始化它的值。因此发生的情况是您正在调用:
null.toString()
这是无效的,并且总是抛出一个
Null指针异常
如果正确,您将得到一个NPE
,因此在您的代码中,您使用{Null}
初始化clientResponse
变量。如果您现在遵循代码的执行方式,您将看到原因:
client.request(
putHeader(…)
end(clientResponse[0].toString())
clientResponse[0]
仍然为空(这是您初始化它的值。因此发生的情况是您正在调用:
null.toString()
这是无效的,将始终引发
空指针异常
我在开始使用Vert-X framework时遇到了相同的问题,我可以从您的代码中注意到以下改进:
.end()
包含新的“客户机请求正文”,但在生成GET时,正文必须为空HttpHeaders
和MediaType
(我个人使用JAX-RS中的MediaType
),而不是编写自己的字符串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时遇到了相同的问题,我可以从您的代码中注意到以下改进:
.end()
包含新的“客户机请求正文”,但在生成GET时,正文必须为空HttpHeaders
和MediaType
(我个人使用JAX-RS中的MediaType
),而不是编写自己的字符串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();
有很多方法可以做同样的事情,你可以检查