IllegalStateException при чтении ответа - PullRequest
0 голосов
/ 05 марта 2020

Я читаю ответ JAVAX, используя метод readEntity(), но получаю следующую трассировку стека:

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();
            }
        }
    }

1 Ответ

0 голосов
/ 05 марта 2020

Вы закрываете свой ответ, наконец, непосредственно перед его возвращением, поэтому вы не можете прочитать его в вызывающем методе 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;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...