Java 读取响应时发生IllegalStateException
我正在使用Java 读取响应时发生IllegalStateException,java,jersey-client,Java,Jersey Client,我正在使用readEntity()方法读取JAVAX响应,但我得到了以下堆栈跟踪: java.lang.IllegalStateException:实体输入流已关闭。 在org.glassfish.jersey.message.internal.EntityInputStream.EnsureRenotClosed(EntityInputStream.java:225)~[jersey common.jar:?] 在org.glassfish.jersey.message.internal.In
readEntity()
方法读取JAVAX响应,但我得到了以下堆栈跟踪:
java.lang.IllegalStateException:实体输入流已关闭。
在org.glassfish.jersey.message.internal.EntityInputStream.EnsureRenotClosed(EntityInputStream.java:225)~[jersey common.jar:?]
在org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:832)~[jersey common.jar:?]
在org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:785)~[jersey common.jar:?]
排队
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(@NonNull final Input xyzInput)
引发永久性异常、暂时性异常{
最终响应res=fetchHealBeamServiceResponse(webTarget,xyzInput);
对象响应MapObject;
Map mapEntityFromResponse=res.readEntity(Map.class);
if(mapEntityFromResponse!=null){
respondentMapObject=mapEntityFromResponse.get(ServiceConstants.MAP\u键);
返回getOutputFromResponseMap(respondentMapObject,xyzInput);
}否则{
抛出新的RuntimeException(“返回的响应不包含映射”);
}
}
私有响应fetchHealBeamServiceResponse(WebTarget healBeamTarget,
输入(输出)
引发永久性异常、暂时性异常{
响应res=null;
试一试{
res=综束目标
.path(HealBeamServiceConstants.GET\u CUSTOMER\u路径)
.register(Configurator.getSoaOpNameFeatureForCustomerResource())
.resolveTemplate(ServiceConstants.ID,xyzInput.getId())
.request(MediaType.APPLICATION\u JSON\u类型)
.property(HealBeamServiceConstants.SERVICE\键、SOA\服务\名称)
.property(HealBeamServiceConstants.OPERATION\u KEY、SOA\u OP\u NAME\u GET\u CUSTOMER)
.acceptLanguage(java.util.Locale.getDefault())
.get();
if(Response.Status.REQUEST\u TIMEOUT.getStatusCode()==res.getStatusInfo().getStatusCode()){
抛出新的TransientException(“请求超时,状态为”+res.getStatusInfo().getStatusCode());
}else if(Response.Status.OK.getStatusCode()!=res.getStatusInfo().getStatusCode()){
log.error(“某些错误”):
}
返回res;
}捕获(运行时异常e){
抛出新的PermanentException(“发生意外异常,异常消息”+e.getMessage());
}最后{
如果(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;
}
在finallysee中设置名称,您的响应也会发生同样的情况,它会关闭然后返回。