我有一个 REST url(使用 DropWizard 用 Java 编码),我想从 PHP 向其发布一个 json。但是我收到415 Unsupported MediaType
错误。我查看了许多论坛,但无法弄清楚错误可能是什么。我的两端代码如下:
服务器
@POST
@Path("/update-table")
@Timed
public TableUpdateResponse updateTable(TestClass testObj) {
LOG.info("inside updateTable function");
//other code ...
}
客户
<?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;
?>
测试类
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 行)。我在这里想念什么?