简体   繁体   中英

Java(Jersey) - Validating a REST GET request for 'results'

I am very new to Java and REST transactions. I apologize if I have not asked this properly, provided what is necessary or did not know how to search for the proper results. Please let me know if anything else is needed to answer this!

I have created a GET request and am now attempting to inspect it programatically to make some magic happen. If this was a regular old result set, the first step that would be to validate that the result set has data in it so that I know my logic wont fail on the next bits of code that run.

I have done some reading and it looks like the calling the readEntity method twice (once in the if statement, once in a the println) at the end of the code that I am working on to try to make this validation happen is causing this error.

Once the readEntity method is called, the input stream gets closed apparently, therefore if I use it for validation its been 'spent' and I seem to not be able to access it for the next logical step.

Can anyone show me a way that I might be able to use to accomplish the goal that I am working for in the if statement at the end of the code?

Thank you!

    Client restClient = ClientBuilder.newClient();
    HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("restun","fift33n1010");
    restClient.register(feature);
    URI baseUri = UriBuilder.fromUri("http://hqidlfitwb01/rest").build();
    WebTarget target = restClient.target(baseUri);
    WebTarget resourceTarget = target.path("find").queryParam("searchFor", "Cabinets").queryParam("query", rackID).queryParam("matchOn", "User Attributes").queryParam("matchUserAttributes","RACKID");
    System.out.println("URI: "+resourceTarget.getUri());
    Response res=resourceTarget.request().get();
    if (!res.readEntity(String.class).equals(GlobalMethods.RESTFAILURE)) {
        System.out.println("  "+res.readEntity(String.class));
    }
    else {
        System.out.println("  Failure - No records!");  
    }

Error Text:

Exception in thread "main" java.lang.IllegalStateException: Entity input stream has already been closed.
at org.glassfish.jersey.message.internal.EntityInputStream.ensureNotClosed(EntityInputStream.java:228)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:854)
at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:808)
at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:326)
at org.glassfish.jersey.client.InboundJaxrsResponse$1.call(InboundJaxrsResponse.java:115)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:419)
at org.glassfish.jersey.client.InboundJaxrsResponse.runInScopeIfPossible(InboundJaxrsResponse.java:267)
at org.glassfish.jersey.client.InboundJaxrsResponse.readEntity(InboundJaxrsResponse.java:112)
at frompmtomaximo.AddEQFromSQL.add(AddEQFromSQL.java:50)
at IntegrationTesting.main(IntegrationTesting.java:22)
String text = res.readEntity(String.class);
if (!text.equals(GlobalMethods.RESTFAILURE)) {
    System.out.println("  "+text);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM