简体   繁体   English

调用 response.getStatus 时,Response 为 NULL,客户端是否未针对 Web 服务中的内容?

[英]Response is NULL when calling response.getStatus, is client not targeting what's in the webservice?

My webservice resource that expects a GET:我需要 GET 的网络服务资源:

 @GET
 @Produces(MediaType.APPLICATION_JSON)
 @Path("/status")
 public Response checkNode() {
        boolean status = !NlpHandler.getHandlerQueue().isEmpty();
        status = status || !NlpFeeder.getInstance().getFiles().isEmpty();

        int statusCode = status ? 200 : 420;
        LOG.debug(
            "checking status - status: " + statusCode
            + ", node: " + this.context.getAbsolutePath()
        );

        return Response.status(statusCode).build();
}

The associated client:相关客户:

public class NodeClient {
    private final Client client;
    private final WebTarget webTarget;

    public NodeClient(String uri) {
        this.uri = "some uri";
        client = ClientBuilder.newCLient();
        webTarget = client.target(uri);

    public synchronized boolean checkNode() throws IOException {
        String path = "status";
        Response response = webTarget
            .path(path)
            .request(MediaType.APPLICATION_JSON)
            .get(Response.class);

        int responseCode = response.getStatus();

        boolean success = responseCode == 200;
        if (!success && responseCode != 420) {
            checkResponse(response);
        }

        return success;
    }
}

In my test, I get a nullpointer at int responseCode = response.getStatus() and I'm fairly sure i'm not getting the response in the right way with webTarget .在我的测试中,我在int responseCode = response.getStatus()得到了一个空指针,我很确定我没有以正确的方式使用webTarget得到响应。 It looks like i'm able to do so correctly with POST responses but not when it's expecting a GET.看起来我可以通过 POST 响应正确地做到这一点,但在期待 GET 时却不能。

    @Test
    public void testCheckNode() throws Exception {
        Response response = Mockito.mock(Response.class);
        Mockito
            .doReturn(response)
            .when(builder)
            .get();

        NodeClient nodeClient;

        Mockito
            .doReturn(200)
            .when(response)
            .getStatus();

        try {
            boolean success = nodeClient.checkNode();

            Assert.assertTrue(success);
        } catch (IOException ex) {
            Assert.fail("No exception should have been thrown");
        }
    }

Any ideas why i'm getting a null response?任何想法为什么我得到空响应?

I think my client clode is fine apparently, the test was wrong.我认为我的客户端 clode 显然很好,测试是错误的。 Originally I was mocking the Response class, now I spied it instead and made the mock return the spy of Response.ok().build()) and that fixed my problem.最初我是在嘲笑Response类,现在我改为监视它并使模拟返回Response.ok().build())的间谍,这解决了我的问题。

    @Test
    public void testCheckNode() throws Exception {
        response = Mockito.spy(Response.class);
        Mockito
            .doReturn(Mockito.spy(Response.ok().build()))
            .when(builder)
            .get(Response.class);

        NodeClient nodeClient;
        PowerMockito
            .doNothing()
            .when(nodeClient, "handleResponse", Mockito.any());

        try {
            boolean success = nodeClient.checkNode();

            Assert.assertTrue(success);
        } catch (IOException ex) {
            Assert.fail("No exception should have been thrown");
        }
    }

暂无
暂无

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

相关问题 调用response.getStatus()时出现NoSuchMethodError - NoSuchMethodError while calling response.getStatus() 为什么response.getStatus()返回200 OK,即使实际响应不是 - Why is response.getStatus() returning 200 OK even when the actual response is not Ksoap响应为空,但Web服务响应具有正确的信息 - Ksoap response null but the webservice response with the correct information 在Java中执行Axis2 Web服务客户端时响应代码为null,但在服务器端成功执行了Web服务 - Response code is null while executing Axis2 webservice client in java but webservice executed successfully at the server end 在使用Apache的HTTP客户端时,将HTTP响应作为字符串的建议方法是什么? - What's the recommended way to get the HTTP response as a String when using Apache's HTTP Client? CXF Web服务客户端,如何处理来自调用的Web服务的响应? - CXF webservice client, how to handle response from a called webservice? 使用wsimport生成的Web服务客户端时获得空响应 - Getting a null response when using a wsimport generated web service client 从Java调用外部Web服务后无法获得SOAP响应 - Not able to get SOAP response after calling external webservice from java 使用JSON响应从Java调用AC#Web服务 - Calling a c# webservice from Java with JSON response JAX-WS WebService 客户端 - “响应:'401:url 未经授权'” - JAX-WS WebService Client - “Response: '401: Unauthorized' for url”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM