2014-09-29 107 views
0

明顯的原因是沒有提供正確的內容類型。但我提供。仍然我得到不受支持的媒體類型。不知道爲什麼。任何幫助不勝感激。Jersey客戶端導致415不支持的媒體類型

Client c = Client.create(); 
WebResource resource = c.resource(HOST+"/test"); 

Gson gson = new Gson(); 
Test test = new Test(); 
test.setTestName("TEST AUTOMATION"); 

resource.header("Content-Type", "Application/json"); 

String testStr = gson.toJson(test); 
System.out.println("Request Str: "+testStr); 
ClientResponse response = resource.post(ClientResponse.class, testStr); 
System.out.println("POST response : "+response); 
POST response : POST http://host:8888/test returned a response status of 415 Unsupported `enter code here`Media Type 
+0

服務器能處理json內容嗎? – 2014-09-29 16:04:04

+0

您已經告訴了請求什麼是內容類型,但是您的服務器上的方法是否說明它支持哪種類型?他們必須匹配。我期望服務器端方法有一個像@ @Consumes(MediaType.APPLICATION_JSON)''的註釋。 – TedTrippin 2014-09-29 16:18:20

+0

我的服務器可以處理json內容。 – 2014-09-30 04:43:04

回答

1

這就是我解決它的方法。它真的很奇怪。直到我結合下面的陳述,它沒有奏效。從我編寫的上述程序中,結合如下的標題聲明和後置聲明。另外不要忘記把charset = UTF-8。

ClientResponse response = resource.header("Content-Type", 
      "application/json;charset=UTF-8").post(ClientResponse.class, 
      testStr); 
+0

即使您刪除charset = UTF-8,我發現它仍在工作 – 2014-10-03 04:29:23

相關問題