我需要使用JUnit測試RESTful webservice *(帶有spring)。我使用測試工具中的IntelliJs構建測試了Webservice,它運行良好。所以現在我使用ApacheHttpClient在該web服務上執行POST請求。即時通訊開始的RESTful Web服務,然後執行該代碼Apache HTTP請求以RESTful web服務的異常終止
CloseableHttpClient httpclient = HttpClients.createDefault();
URI uri = new URIBuilder()
.setScheme("http")
.setHost("localhost")
.setPort(8080)
.setPath("/register")
.build();
HttpPost post = new HttpPost(uri);
List<NameValuePair> nvps = new ArrayList<>();
nvps.add(new BasicNameValuePair("name", "test"));
nvps.add(new BasicNameValuePair("email", "test"));
nvps.add(new BasicNameValuePair("password", "test"));
post.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response = httpclient.execute(post);
response.close();
但這說結束在該web服務的異常:
甲servlet請求的URI http://localhost:8080/register包含在請求體但形式參數請求主體已被servlet或訪問請求參數的servlet過濾器使用。只有使用@FormParam的資源方法才能按預期工作。通過其他方式使用請求主體的資源方法將無法按預期工作。 UT005023:異常處理請求/錯誤
完整堆棧跟蹤:http://pastebin.com/96aJtaS5
*完整的源代碼,請訪問:https://github.com/pkalauner-tgm/dezsys09-java-webservices THX對您有所幫助
難道這是由於在讀取它的正文之前調用close()的響應嗎? – Roman
響應對我來說實際上並不重要,所以我關閉它直到請求被執行。正如我所說的,異常出現在web服務上而不是這個代碼上。 PS:在這個URL的請求應該創建一個用戶並保存它,以便響應可以正常或不正確或某物。像那樣 – LimitX