[英]Google Cloud Endpoint and Objectify
我定义了一个使用Objectify
处理数据存储的Google Cloud Endpoints。 问题是我的模型使用objectify com.googlecode.objectify.Key
类。
@Entity
public class RealEstateProperty implements Serializable {
@Id
private Long id;
@Parent
private Key<Owner> owner;
private String name;
private Key<Address> address;
}
在我的端点中,我定义了一种创建RealEstateProperty
的方法:
@ApiMethod(name = "create", path = "properties", httpMethod = HttpMethod.POST)
public void create(RealEstateProperty property, User user) throws Exception {
}
在API Explorer
, create
方法需要一个表示地址Key
的字符串。 问题是我想提供地址而不是Key
。
是否可以使用objectify
创建端点? 如果是这样,您如何设计数据模型来处理Key
?
您可以创建一个用于通过API(端点)进行通信的类,该类包含一个地址字段而不是键字段:
public class RealEstatePropertyAPI implements Serializable {
private Long id;
private Key<Owner> owner;
private String name;
private Address address;
}
并在您的端点中:
@ApiMethod(name = "create", path = "properties", httpMethod = HttpMethod.POST)
public void create(RealEstatePropertyAPI propertyAPI, User user) throws Exception {
//ie: get address from propertyAPI and find in datastore or create new one.
}
或仅向端点添加另一个参数。
是的,似乎端点不支持Objectify Keys。 这也给我带来了一些问题。 为了避免在Maven构建中引发错误,我对Key属性进行了注释,以使端点https://developers.google.com/appengine/docs/java/endpoints/annotations忽略该属性:
@ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
使用端点添加新的RealEstateProperty时,请在端点中使用String参数创建Address对象。 将新的Address对象作为参数传递给RealEstateProperty构造函数,并在构造函数中创建和分配键。
@Entity
public class RealEstateProperty implements Serializable {
@Id
private Long id;
@Parent
private Key<Owner> owner;
private String name;
@ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
private Key<Address> address;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.