我正在使用GoogleAppEngine1.9.3,Eclipse,Objectify5.03。我的班级如下:
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不支持
这是我第一次尝试物化。
我做错了什么。从我到目前为止所读到的,GAE端点和对象化应该能工作,对吗?
发布于 2014-07-07 19:28:49
Google无法序列化Ref对象,因为它是由objectify定义的任意对象,因此不支持错误指示的对象。
这是已知的具有云端点的限制,因为它不允许使用自定义对象。如果您感兴趣的话,这里有一个关于这一点的讨论主题: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
}但在我看来,一个更好的方法是重新考虑你的班级的设计,或者更确切地说,重新考虑你想要做的事情。
发布于 2014-07-24 14:30:30
ApiResourceProperty注释不适用于Google Emdpoints+Objectify组合,因为Ref或Key是对象化的特定类,而Google不识别它们,在尝试生成客户端库时会出现错误。我更改了用户类,如下所示。
@Id private Long userId;
@Index private String userName;
@Load private UserDetails userDetails;
@Load private UserPassword userPassword;
@Load private ArrayList<Account> userAccounts;
//getters and setters当我按用户名检索用户时,我通过getter获得用户、UserDetails、UserPassword以及用户帐户列表(一次)
@ApiMethod(name = "getUserByName", path = "get_user_by_name")
public User getUserByName(@Named("userName") String userName) {
User user = null;
try {
user = ofy().load().type(User.class).filter("userName", userName).first().now();
if(user != null)
log.info("UserEndpoint.getUserByName...user retrieved="+user.getUserId());
else
log.info("UserEndpoint.getUserByName...user is null");
} catch(Exception e) {
log.info("UserEndpoint.getUserByName...exception="+e.getMessage());
}
return user;
}当我使用Google上的Datastore Viewer查看数据时,我会在User表中的userDetails、userPassword和Accounts列中看到一些条目。我假设这些都是对各自表中的实际数据的引用,而不是数据本身的副本。希望这能有所帮助。
https://stackoverflow.com/questions/24590518
复制相似问题