[英]What am I missing. I want to check the response code of http Akka before processing JSON result
I am new to Akka and java 8. So I am using pipe to call this method to call a REST service. 我是Akka和java 8的新手。所以我使用管道调用此方法来调用REST服务。 It all works ok but would like to check response code before unmarshling json. 一切正常,但想在取消json之前检查响应代码。 or abort if response code is != 200. And is it correct with all these return null results? 或者如果响应代码是!= 200则中止。并且所有这些返回null结果是否正确? ........ ........
My sample code: 我的示例代码:
CompletionStage<HttpResponse> fetch(LoginUserRequest input) {
CompletionStage<HttpResponse> result = null;
*some json code*
result = http.singleRequest(HttpRequest.POST(url).withEntity(HttpEntities.create(ContentTypes.APPLICATION_JSON, jsonString)));
result.thenCompose(response ->
response.entity().toStrict(10000, materializer))
.thenApply(entity -> {
log.info("före");
CompletionStage<UserResult> userResult = Jackson.unmarshaller(UserResult.class).unmarshal(entity, materializer);
userResult.thenApply(unResult -> {
requester.tell(new LoginUserResponse(unResult.getCredentials().getToken() != null, unResult.getCredentials().getToken()), self());
return unResult;
});
return null;
})
.exceptionally(t -> {
log.error("Exception occurred: " + t.getMessage());
return null;
});
return result;
}
You have the response code on your response
object above. 您的response
对象上面有响应代码。 So you can check that and act accordingly. 所以你可以检查并采取相应的行动。 Within your first .thenApply
, the return null should be wrong, since the result of userResult.thenApply
wont be returned this way. 在你的第一个.thenApply
,返回null应该是错误的,因为userResult.thenApply
的结果不会以这种方式返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.