1
我正在使用Jetty服務器,Jersey庫和JAX-RS學習REST服務。返回JSON響應
我有以下的方法,該方法應該返回(在XML或JSON格式)所有客戶對象:
@GET
@Produces({ "application/xml", "application/json" })
public Collection<Customer> getAll() {
List<Customer> customerList = new ArrayList<Customer>(customerDB.values());
return customerList;
}
客戶對象定義爲:
package com.rest.domain;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
@XmlRootElement(name = "customer")
public class Customer {
// Maps a object property to a XML element derived from property name.
@XmlElement
public int id;
@XmlElement
public String firstname;
@XmlElement
public String lastname;
@XmlElement
public String email;
}
如果我發送如下curl命令我收到一個xml響應(而不是json,請求):
curl -H "Content-Type: application/json" -X GET http://localhost:8085/rest/customers/
爲什麼它返回一個xml響應,如果我要求json?
謝謝。就是這樣! :) – TheAptKid