java - 读取响应时出现 IllegalStateException
问题描述
我正在使用readEntity()
方法读取 JAVAX 响应,但我得到以下堆栈跟踪:
java.lang.IllegalStateException: Entity input stream has already been closed.
at org.glassfish.jersey.message.internal.EntityInputStream.ensureNotClosed(EntityInputStream.java:225) ~[jersey-common.jar:?]
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:832) ~[jersey-common.jar:?]
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:785) ~[jersey-common.jar:?]
在线
Map<String, Map> mapEntityFromResponse = res.readEntity(Map.class);
这是我的代码
public Output getClaimsFromAPI(@NonNull final Input xyzInput)
throws PermanentException, TransientException {
final Response res = fetchHealBeamServiceResponse(webTarget, xyzInput);
Object respondentMapObject;
Map<String, Map> mapEntityFromResponse = res.readEntity(Map.class);
if (mapEntityFromResponse != null) {
respondentMapObject = mapEntityFromResponse.get(ServiceConstants.MAP_KEY);
return getOutputFromResponseMap(respondentMapObject, xyzInput);
} else {
throw new RuntimeException("The response returned does not contain map");
}
}
private Response fetchHealBeamServiceResponse(WebTarget healBeamTarget,
Input xyzInput)
throws PermanentException, TransientException {
Response res = null;
try {
res = healBeamTarget
.path(HealBeamServiceConstants.GET_CUSTOMER_PATH)
.register(Configurator.getSoaOpNameFeatureForCustomerResource())
.resolveTemplate(ServiceConstants.ID, xyzInput.getId())
.request(MediaType.APPLICATION_JSON_TYPE)
.property(HealBeamServiceConstants.SERVICE_KEY, SOA_SERVICE_NAME)
.property(HealBeamServiceConstants.OPERATION_KEY, SOA_OP_NAME_GET_CUSTOMER)
.acceptLanguage(java.util.Locale.getDefault())
.get();
if (Response.Status.REQUEST_TIMEOUT.getStatusCode() == res.getStatusInfo().getStatusCode()) {
throw new TransientException("Request timed out with status" + res.getStatusInfo().getStatusCode());
} else if (Response.Status.OK.getStatusCode() != res.getStatusInfo().getStatusCode()) {
log.error("Some Error"):
}
return res;
} catch (RuntimeException e) {
throw new PermanentException("Unexpected Exception Occured, Exception Message " + e.getMessage());
} finally {
if (res != null) {
res.close();
}
}
}
解决方案
您最终在返回之前关闭您的响应,这就是为什么您无法在调用方法 getClaimsFromAPI() 中读取它的原因。只是为了演示:您认为下面发布的方法 main() 会打印什么?
public class NewApp {
public static void main(String[] args) {
Person p = demonstrate();
System.out.println(p.name);
}
public static Person demonstrate(){
Person person = new Person();
try {
person.name = "name set in try";
return person;
} catch (Exception ex) {
throw ex;
} finally {
person.name = "name set in finally";
}
}
}
class Person {
public String name;
}
推荐阅读
- c++ - 第一次使用可变参数函数
- python - 如何在numpy数组列中查找数字序列
- node.js - 在共享的 Observable 上引发事件
- html - 如何在将鼠标悬停在其后的元素上时更改元素的 css?
- javascript - 我在 MySQL Hosting 中创建的数据库正在形成一个表,其中包含来自 JavaScript 中的 API 的空数据
- java - 如何以编程方式检测 java.lang.OutOfMemoryError: Direct buffer memory to create a java heap dump?
- python - python list问题-为什么函数会产生淡蓝色?-vscode
- amazon-web-services - ACL 配置
- python - 偏斜正态分布(偏斜与 alpha 或参数) - 生成 N 个整数(Python SciPy)
- swift - 如何解决 Xcode 非法指令:4 错误?