如何返回Objectify上的自定义对象列表 [英] How to return a list of custom objects on Objectify

查看:91
本文介绍了如何返回Objectify上的自定义对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个使用Google App Engine作为后端的Android项目,如下所述:使用Android&在Android Studio上的Google App Engine



我在后端有一些模型类,像User和Item,我试图返回一个Items用户拥有。

  public List< Ref< Item>> getItems(){
返回项目;
}

当我尝试与Gradle文件同步项目,我得到这个错误:


错误:任务':backend:appengineEndpointsGetClientLibs'的执行失败。
运行endpoints命令时发生错误get-client-lib:参数化类型com.googlecode.objectify.Ref≤backend.model.Item>不支持。

我在这里查了一些其他的问题,并且通过添加 @ApiResourceProperty(ignored = AnnotationBoolean.TRUE)注解来构建项目而没有错误我的getter方法。但添加完这一行后,我无法在Android应用程序端看到此方法。



任何想法如何使获得Android端项目列表成为可能? p>

解决方案

我通过保存/检索包含序列化集合的对象来完成此操作。 Class Lesson实现了Serializable。

Language.java

  import java.io.Serializable; 
import java.util.List;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Serialize;

@Entity
公共类语言{

@Id
私有字符串键;
私有字符串标题;
@Serialize
私人列表<课程>教训; //这里收集

// getters / setters ommited
}

LanguageService.java

  import static com.googlecode.objectify.ObjectifyService.ofy; 

导入com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.Named;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.minspok.entity.Language;
$ b @Api(name =langapi,version =v1,description =langapi)

public class LanguageService {

static {
ObjectifyService.register(Language.class);


$ b @ApiMethod(name =get)
public语言getLanguage(@Named(key)字符串键){
语言语言= ofy()。load()。key(Key.create(Language.class,
key))。now();
返回语言;


$ b @ApiMethod(name =create)
public void createLanguage(语言语言){
ofy()。save()。实体(语言);


有用阅读: https://github.com/objectify/objectify/wiki/Entities


I'm working on an Android project which uses Google App Engine for backend as described here: Using Android & Google App Engine on Android Studio.

I have some model classes on the backend side like User and Item, and I'm trying to return a list of Items user has.

public List<Ref<Item>> getItems() {
    return items;
}

When I try to Sync Project with Gradle Files, I get this error:

Error:Execution failed for task ':backend:appengineEndpointsGetClientLibs'. There was an error running endpoints command get-client-lib: Parameterized type com.googlecode.objectify.Ref≤backend.model.Item> not supported.

I checked some other questions here and was able to build the project without errors by adding @ApiResourceProperty(ignored = AnnotationBoolean.TRUE) annotation to my getter method. But after adding this line, I cannot see this method on Android app side.

Any idea how to make it possible to get a list of Items on Android side?

解决方案

I did it by saving/retrieving object that contains serialized collection. Class Lesson implements Serializable.

Language.java

import java.io.Serializable;
import java.util.List;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Serialize;

@Entity
public class Language {

    @Id
    private String key;
    private String title;
    @Serialize
    private List<Lesson> lessons;  //here collection

    //getters/setters ommited
}

LanguageService.java

import static com.googlecode.objectify.ObjectifyService.ofy;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.Named;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.ObjectifyService;
import com.minspok.entity.Language;

@Api(name = "langapi", version = "v1", description = "langapi")

public class LanguageService {

    static{
        ObjectifyService.register( Language.class );
    }


    @ApiMethod(name = "get")
    public Language getLanguage(@Named("key") String key){
        Language language = ofy().load().key(Key.create(Language.class,  
                        key)).now();
        return language;
    }


    @ApiMethod(name = "create")
    public void createLanguage(Language language){
        ofy().save().entity(language);   
    }
}

Helpful reading: https://github.com/objectify/objectify/wiki/Entities

这篇关于如何返回Objectify上的自定义对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆