我花了半天时间疯狂地让我的 Jersey 服务接受和操作 JSON。
这是我正在做的事情: 在 PHP 中使用 Zend 框架:
$client = new Zend_Http_Client("http://localhost:8080/api/");
$data = array("city"=> "Paris", "zip" => "1111");
$json = json_encode($data);
$client->setHeaders("Content-type", "application/json");
$client->setRawData($json, 'application/json')->request("GET");
接口方法:
@GET
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response getAPI( Address addr) {
JSONObject out = new JSONObject();
out.put("city test",addr.getCity());
Response response = null;
return response.ok(out.toString()).header("Accept", "application/json").build();
}
在一个单独的文件中,我有我的注释类:
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Address
{
@XmlElement(name="city")
public String city;
@XmlElement(name="zip")
public String zip;
public String getCity() {
return city;
}
}
我收到不支持的媒体类型错误:
Zend_Http_Response Object
(
[version:protected] => 1.1
[code:protected] => 415
[message:protected] => Unsupported Media Type
[headers:protected] => Array
(
[Server] => Apache-Coyote/1.1
[Content-type] => text/html;charset=utf-8
[Content-length] => 1117
[Date] => Tue, 29 May 2012 17:55:03 GMT
[Connection] => close
)
[body:protected] =>
我错过了什么?
谢谢大家,丹尼尔