简体   繁体   中英

Google Cloud Endpoint and Objectify

I defined a Google Cloud Endpoints that uses Objectify to deal with the datastore. The issue is that my model uses the objectify com.googlecode.objectify.Key class.

@Entity
public class RealEstateProperty implements Serializable {

    @Id
    private Long id;
    @Parent
    private Key<Owner> owner;
    private String name;
    private Key<Address> address;

}

In my endpoint I defined a method to create a RealEstateProperty :

@ApiMethod(name = "create", path = "properties", httpMethod = HttpMethod.POST)
public void create(RealEstateProperty property, User user) throws Exception {
    }

In the API Explorer , the create method expects a string representing a Key for the address. The issue is that I'd like to provide the address and not the Key .

Is it possible to create an endpoint with objectify ? If so, how do you design your data model to deal with Key ?

You can create a class for communication via API (endpoint) that contain an address field instead Key field:

public class RealEstatePropertyAPI implements Serializable {

    private Long id;
    private Key<Owner> owner;
    private String name;
    private Address address;

}

and in your endpoint:

@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.
}

or just add another parameter to your endpoint.

Yes it seems endpoints don't support Objectify Keys. This caused a few issues for me too. To avoid the errors thrown in the maven build I annotated the Key property to be ignored by the Endpoint https://developers.google.com/appengine/docs/java/endpoints/annotations :

 @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)

When adding a new RealEstateProperty using your endpoint, create your Address object with a String argument in your endpoint. Pass your new Address object as an argument to your RealEstateProperty constructor and create and assign the key within the constructor.

@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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM