2012-12-13 38 views
0

我有一個REST URL(使用DropWizard編碼),我想從PHP POST JSON。但是我得到415 Unsupported MediaType錯誤。我查了很多論壇,我無法弄清楚錯誤可能是什麼。我在兩端代碼如下:POST JSON到REST從PHP

服務器

@POST 
@Path("/update-table") 
@Timed 
public TableUpdateResponse updateTable(TestClass testObj) { 
    LOG.info("inside updateTable function"); 
    //other code ... 
} 

CLIENT

<?php 

$url = "localhost:8084/update-table" 
$ch = curl_init($url); 

$jsonString = array("testString" => "Hello World!");    
$json = json_encode($jsonString); 

$headers = array(); 
$headers[] = 'Content-Type: application/json'; 

curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $json); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

$response = curl_exec($ch); 
echo $response; 

?> 

的TestClass

public class TestClass { 
    private String testString; 

    public TestClass(String testString) { 
     super(); 
     this.testString = testString; 
    } 

    public TestClass() { 
     super(); 
    } 

    public String getTestString() { 
     return testString; 
    } 

    public void setTestString(String testString) { 
     this.testString = testString; 
    } 

} 

請求未達到REST(LOG行不打印)。我在這裏錯過了什麼?

回答

0

嘗試更改應用程序/ JSON內容類型的這些

application/json 
application/x-javascript 
text/javascript 
text/x-javascript 
text/x-json 

Altough在RFC指定正確的一個應用程序/ JSON,Java代碼可能有一個問題,它

+0

我想問題是與PHP代碼,因爲,我嘗試從終端curl命令,它的工作原理。 – Neo

0

加註釋@Consumes(MediaType.APPLICATION_JSON)