Sydney
Sydney

Reputation: 12212

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?

Upvotes: 2

Views: 1922

Answers (2)

emjrose
emjrose

Reputation: 112

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;

}

Upvotes: 3

Mateusz
Mateusz

Reputation: 1224

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.

Upvotes: 3

Related Questions