eclipse - GAE+Objectify - 不支持参数化类型 com.googlecode.objectify.Ref

标签 eclipse google-app-engine google-cloud-endpoints objectify

我正在使用 Google App engine1.9.3、Eclipse、Objectify5.03。我的类(class)如下:

import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Load;

@Entity
public class User {

@Id private Long userId;
private String userName;
@Load private Ref<UserDetails> userDetails;
@Load private Ref<UserPassword> userPassword;

//getters & setters 

}

当我尝试通过 Eclipse 为此类创建 google 端点时,出现以下错误: java.lang.IllegalArgumentException:不支持参数化类型 com.googlecode.objectify.Ref

这是我第一次尝试 Objectify。

任何想法我做错了什么。从我目前所读的内容来看,GAE 端点和 Objectify 应该可以工作,对吗?

最佳答案

Google Cloud Endpoints 无法序列化 Ref 对象,因为它是由 objectify 定义的任意对象,因此如错误所示不受支持。

这是 Cloud Endpoints 的已知限制,因为它不允许使用自定义对象。如果您有兴趣,请特别关注这一点:Cloud endpoints .api generation exception when using objectify (4.0b1) parameterized key

您必须使用 @ApiResourceProperty 注释您的方法并将其忽略的属性设置为 true,如下面的代码所示:

import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Load;
import com.google.api.server.spi.config.AnnotationBoolean;
import com.google.api.server.spi.config.ApiResourceProperty;

@Entity
public class User 
{
    @Id private Long userId;
    private String userName;
    @Load private Ref<UserDetails> userDetails;
    @Load private Ref<UserPassword> userPassword;

    //getters & setters
    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE) 
    public UserDetail getUserDetails(){
    }

    @ApiResourceProperty(ignored = AnnotationBoolean.TRUE) 
    public UserPassword getUserPassword(){
    }
}

如果您仍想使用这些对象中保存的数据,请考虑在您的类中添加一些字段来保存数据,并在您的 User 类完成加载后初始化它们,如下所示:

@Ignore String firstName;
@OnLoad
void trackUserDetails() 
{ 
    this.firstName = getUserDetails().getFirstName(); 
    // add more code here to set other fields, you get the gist
}

但在我看来,更好的方法是重新考虑您的类(class)设计,或者更确切地说,重新考虑您要尝试做什么。

关于eclipse - GAE+Objectify - 不支持参数化类型 com.googlecode.objectify.Ref,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24590518/

相关文章:

java - 如何在 Eclipse 中使用自动完成添加完全限定名称

java - 当打开 RTC 'Jazz Administration' 透视图时,Eclipse JVM 退出代码 127

eclipse - 使用 Eclipse 在 PyDev 中调试的快捷方式是什么

python - 如何在 App Engine 中使用导入内存缓存的库

eclipse - App Engine Dev App Server 不会在 Eclipse 中更新

java.util.Logger 打包在不同操作系统中时的行为有所不同

google-app-engine - 为什么在 Go AppEngine aetest 中使用 datastore.Property List 获取 datastore.Put 的 "invalid entity type"?

java - 为什么 Google Cloud Endpoints 不支持简单的对象返回类型?

java - 对象化 v5 : Profile is not a supported property type

google-app-engine - 连接到 Google Cloud SQL