我创建了一个 API 后端:
@ApiMethod(name = "create", path = "properties", httpMethod = HttpMethod.POST)
public void create(RealEstatePropertyAPI property, User user)
throws Exception {
}
使用以下数据模型:
public class RealEstatePropertyAPI {
private Long id;
private String name;
private AddressAPI address;
public RealEstatePropertyAPI() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AddressAPI getAddress() {
return address;
}
public void setAddress(AddressAPI address) {
this.address = address;
}
}
public class AddressAPI {
private Long id;
private String street;
private String city;
private String state;
private String zip;
private String country;
public AddressAPI() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZip() {
return zip;
}
public void setZip(String zip) {
this.zip = zip;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
在API Explorer
以下请求中有效:
POST http://localhost:8888/_ah/api/realestate/v1/properties
Content-Type: application/json
X-JavaScript-User-Agent: Google APIs Explorer
{
"name": "test",
"address": {
"city": "DC"
}
}
但是在使用Angular JS
客户端时,RealEstatePropertyAPI
会创建实例但不会填充字段(全部null
)。请求是:
[{"jsonrpc":"2.0","id":"gapiRpc","method":"realestate.create","params":{"resource":"{\"name\": \"Test\",\"address\": { \"street\": \"White House\"}}"},"apiVersion":"v1"}]
JS调用:
var x = '{"name": "Test","address": { "street": "White House"}}';
gapi.client.realestate.create({"resource": x}).execute(function(resp) {
console.log(resp);
});