简体   繁体   English

Java(Jersey)-验证“结果”的REST GET请求

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

I am very new to Java and REST transactions. 我对Java和REST事务非常陌生。 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. 我创建了一个GET请求,现在正尝试以编程方式检查它以使某些魔术发生。 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. 我已经做过一些阅读,看起来好像在我正在尝试使此验证发生的代码末尾两次调用readEntity方法(一次在if语句中,一次在println中)导致此错误。

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. 调用readEntity方法后,输入流将明显关闭,因此,如果我将其用于验证已被“花费”,则似乎无法在下一个逻辑步骤中访问它。

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? 谁能在代码末尾的if语句中向我展示一种我可以用来实现我正在努力实现的目标的方法?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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