Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objectify with Cloud Endpoints

I am using appengine cloud endpoints and objectify. I have previously deployed these endpoints before and now I am updating them and it is not working with Objectify. I have moved to a new machine and running latest appengine 1.8.6. Have tried putting objectify in the classpath and that did not work. I know this can work, what am I missing??

When running endpoints.sh:

Error: Parameterized type
   com.googlecode.objectify.Key<MyClass> not supported.

UPDATE: I went back to my old computer and ran endpoints.sh on same endpoint and it worked fine. Old machine has 1.8.3. I am using objectify 3.1.

UPDATE 2: Updated my old machine to 1.8.6 and get same error as other machine. Leaves 2 possibilities: 1) Endpoints no longer support objectify 3.1 or 2) Endpoints have a bug in most recent version

Most likely #1...I've been meaning to update to 4.0 anyways...

like image 979
Patrick Avatar asked Oct 31 '13 13:10

Patrick


People also ask

What is Cloud Endpoints?

Cloud Endpoints uses an NGINX-based proxy and distributed architecture for performance and scale. Using an OpenAPI Specification or one of our API frameworks, Cloud Endpoints gives you the tools you need for API development and provides insight with Cloud Logging, Cloud Monitoring, and Cloud Trace.

What is endpoint in GCP?

Endpoints is an API management system that helps you secure, monitor, analyze, and set quotas on your APIs using the same infrastructure Google uses for its own APIs.


2 Answers

Because of the popularity of Objectify, a workaround was added in prior releases to support the Key type, until a more general solution was available. Because the new solution is available, the workaround has been removed. There are two ways you can now approach the issue with the property.

  1. Add an @ApiResourceProperty annotation that causes the key to be omitted from your object during serialization. Use this approach if you want a simple solution and don't need access to the key in your clients.
  2. Add an @ApiTransformer annotation that provides a compatible mechanism to serialize/deserialize the field. Use this approach if need access to the key (or a representation of it) in your clients. As this requires writing a transformer class, it is more work than the first option.
like image 181
Dan Holevoet Avatar answered Oct 23 '22 07:10

Dan Holevoet


I came up with the following solution for my project:

@Entity
public class Car {

    @Id Long id;

    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)
    Key<Driver> driver;

    public Key<Driver> getDriver() {
        return driver;
    }

    public void setDriver(Key<Driver> driver) {
        this.driver = driver;
    }

    public Long getDriverId() {
        return driver == null ? null : driver.getId();
    }

    public void setDriverId(Long driverId) {
        driver = Key.create(Driver.class, driverId);
    }
}

@Entity
public class Driver {
    @Id Long id;
}

I know, it's a little bit boilerplate, but hey - it works and adds some handy shortcut methods.

like image 44
Flori Avatar answered Oct 23 '22 07:10

Flori