【问题标题】:Waiting for POST to finish processing before calling GET Request在调用 GET 请求之前等待 POST 完成处理
【发布时间】:2016-08-04 21:20:14
【问题描述】:

我正在做一个集成测试,我调用一个将文件发送到服务器的 POST 请求。在 POST 之后,我立即调用一个 GET 请求来检索文档并获得一个响应,该响应为我提供了该文档的属性。但是,有时我会收到 404 错误,说文档不存在,直到我在几秒钟后再次运行它。

我认为 POST 还没有完成处理,所以我实现了一个 Thread.sleep 来等待 10 秒,但这种方式似乎并不好,因为它可能等待的时间比它需要的时间长,或者可能等待的时间不够长。是否有某种“隐式等待”允许 POST 在请求 GET 之前完成处理?

下面是代码sn-p:

@Test
public void PostDocumentThenCheckIfDocumentExistThenRemove() throws IOException, InterruptedException {     
    try {
        String str = fileToStringProcessing("C:/Users/Linh/Desktop/file.xml");
        ResponseEntity<Message> postResponse = getRestTemplate().exchange(getUri() + "documents", HttpMethod.POST, new HttpEntity(str, getHeaders()), Message.class);
        Thread.sleep(10000);
        ResponseEntity<Account> getResponse = getRestTemplate().exchange(getUri() + "account/7452616052/documents?start=2015-01-01&end=2016-03-31", HttpMethod.GET, getHttpEntity(), Account.class);
        ResponseEntity<Message> deleteResponse = getRestTemplate().exchange(getUri() + "documents/file.xml", HttpMethod.DELETE, getHttpEntity(), Message.class);

        assertThat(postResponse.getStatusCode(), is(HttpStatus.CREATED));
        assertThat(getResponse.getStatusCode(), is(HttpStatus.OK));
        assertThat(deleteResponse.getStatusCode(), is(HttpStatus.OK));
    }catch(HttpClientErrorException e) {
        fail("Error! Status code " + e.getStatusCode());
    }   
}

这是控制台中的 404 错误。您可以在此处看到 POST 成功但 GET 不成功。之后执行 GET,几秒钟后,将显示成功

11:56:32.700 [main] DEBUG o.s.web.client.RestTemplate - POST request for "https://dpdev.billing.com/tf/dp/documents" resulted in 201 (Created)
11:56:32.700 [main] DEBUG o.s.web.client.RestTemplate - Reading [com.digitalplatform.model.Message] as "application/xml" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@1dac5ef]
11:56:32.904 [main] DEBUG o.s.web.client.RestTemplate - Created GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31"
11:56:32.914 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
11:56:33.690 [main] DEBUG o.s.web.client.RestTemplate - GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31" resulted in 404 (Not Found); invoking error handler

GET 和 DELETE 请求在 404 之后:

12:00:21.383 [main] DEBUG o.s.web.client.RestTemplate - Created GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31"
12:00:21.444 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
12:00:23.176 [main] DEBUG o.s.web.client.RestTemplate - GET request for "https://dpdev.billing.com/tf/dp/account/7452616052/documents?start=2015-01-01&end=2016-03-31" resulted in 200 (OK)
12:00:23.176 [main] DEBUG o.s.web.client.RestTemplate - Reading [com.digitalplatform.model.Account] as "application/xml" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@37271612]
12:00:23.380 [main] DEBUG o.s.web.client.RestTemplate - Created DELETE request for "https://dpdev.billing.com/tf/dp/documents/file.xml"
12:00:23.381 [main] DEBUG o.s.web.client.RestTemplate - Setting request Accept header to [application/xml, text/xml, application/json, application/*+xml, application/*+json]
12:00:25.120 [main] DEBUG o.s.web.client.RestTemplate - DELETE request for "https://dpdev.billing.com/tf/dp/documents/file.xml" resulted in 200 (OK)
12:00:25.120 [main] DEBUG o.s.web.client.RestTemplate - Reading [com.digitalplatform.model.Message] as "application/xml" using [org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@37271612]

【问题讨论】:

    标签: java spring rest testing junit


    【解决方案1】:

    REST 服务是否异步接收请求。如果没有,那么当您发出下一个 GET 请求时该文档应该可用。 HTTP 线程阻塞,直到您收到 POST 的响应。 POST 可能返回错误代码。在发出 get 之前,首先检查 postResponse 中的状态代码。

        String str = fileToStringProcessing("C:/Users/Linh/Desktop/file.xml");
        ResponseEntity<Message> postResponse = getRestTemplate().exchange(getUri() + "documents", HttpMethod.POST, new HttpEntity(str, getHeaders()), Message.class);
        if(postResponse.getStatusCode() == HttpStatus.OK){ or other success code
           ResponseEntity<Account> getResponse = getRestTemplate().exchange(getUri() + "account/7452616052/documents?start=2015-01-01&end=2016-03-31", HttpMethod.GET, getHttpEntity(), Account.class);
           ResponseEntity<Message> deleteResponse = getRestTemplate().exchange(getUri() + "documents/2015067452616054", HttpMethod.DELETE, getHttpEntity(), Message.class);
        }
        else{
           //process error response
        }
    

    【讨论】:

    • 用完整代码更新了问题。可能应该提出整个方法。 POST 时,我得到 200 OK 状态,所以 POST 肯定是成功的。之后还会更新控制台信息
    猜你喜欢
    • 1970-01-01
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 1970-01-01
    • 2012-04-11
    • 1970-01-01
    • 2013-01-22
    相关资源
    最近更新 更多