读取响应时出现IllegalStateException

问题描述 投票:0回答:1

我正在使用readEntity()方法读取JAVAX响应,但正在跟踪stacktrace:

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();
            }
        }
    }
java jersey-client
1个回答
0
投票

您最终会在返回响应之前立即关闭响应,这就是为什么无法在调用方法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;
}
© www.soinside.com 2019 - 2024. All rights reserved.