1

在开发人员网站上的所有示例之后,我正在使用 GAE Cloud 端点和 Datastore。生成客户端库时出现无法解决的错误:

“线程“主”java.lang.IllegalArgumentException 中的异常:不支持参数化类型接口 java.lang.Iterable...”

谁能告诉我为什么会收到错误以及如何解决?

这是我的代码:

**Util.java**
package com.davozardini.myfisrtapp;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.FilterOperator;

/**
 * This is the utility class for entity operation methods.
 * 
 */
public class Util {

private static final Logger logger = Logger.getLogger(Util.class.getCanonicalName());
private static DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();


/**
* 
* @param entity  : entity to be persisted
*/
 public static void persistEntity(Entity entity) {
    logger.log(Level.INFO, "Saving entity");
    datastore.put(entity);      
 }


/**
* Delete the entity from persistent store represented by the key
* @param key : key to delete the entity from the persistent store
*/
 public static void deleteEntity(Key key) {
    logger.log(Level.INFO, "Deleting entity");
    datastore.delete(key);      
  }

 /**
 * Search and return the entity from datastore.
 * @param key : key to find the entity
 * @return  entity
 */
 public static Entity findEntity(Key key) {
    logger.log(Level.INFO, "Search the entity");
    try {     
      return datastore.get(key);
    } catch (EntityNotFoundException e) {
      return null;
    }
  }

 /***
     * Search entities based on search criteria
     * @param kind
     * @param searchBy
     *            : Searching Criteria (Property)
     * @param searchFor
     *            : Searching Value (Property Value)
     * @return List all entities of a kind from the cache or datastore (if not
     *         in cache) with the specified properties
     */
 public static Iterable<Entity> listEntities(String kind) {
    logger.log(Level.INFO, "Search entities based on search criteria");

    Query q = new Query(kind);              
    PreparedQuery pq = datastore.prepare(q);
    return pq.asIterable();
  }


}

Person.java 包 com.davozardini.myfisrtapp;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.davozardini.myfisrtapp.Util;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.AnnotationBoolean;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import javax.inject.Named;

@Api(
    name = "Person",
    version = "v1",
    description = "myfisrtapp Person API",      
    defaultVersion = AnnotationBoolean.TRUE
)


/**
 * This class handles all the CRUD operations related to
 * People entity.
 *
 */
public class Person {


/**
 * Get all Person entities
 * @return: Iterable<Entity> entities
 */
@ApiMethod(
        name = "Person.getAllPersons",
        path = "Person",
        httpMethod = HttpMethod.GET
     )
 public static Iterable<Entity> getAllPersons() {
     Iterable<Entity> entities = Util.listEntities("Person");
     return entities;       
 }

 /**
   * Get Person entity
   * @param name : key of the Person
   * @return: Person entity
   */
  public static Entity getPerson(@Named("name")String name) {
    Key key = KeyFactory.createKey("Person",name);
    return Util.findEntity(key);
  }

/**
 * Update the product
 * @param name: name of the product
 * @param description : description
 * @return  updated product
 */
  public static void createPerson(@Named("name")String name, @Named("description")String description) {
    Entity person = new Entity("Person");
    person.setProperty("name", name);
    person.setProperty("description", description);
    Util.persistEntity(person);
  }       

}
4

3 回答 3

1

端点返回类型应该是 JavaBean。注意:不支持返回简单类型,例如 String 或 int。返回值需要是 JavaBean(非参数化)、数组或集合。CollectionResponse (com.google.api.server.spi.response.CollectionResponse) 或其子类有特殊处理。通常,当前不支持参数化 bean。

于 2016-05-05T04:41:51.813 回答
0

请尝试将这些导入 java.lang.iterable 、 com.google.common.collect.Iterables 添加到您的 util.java

于 2013-07-06T19:08:16.503 回答
0

参数化类型(如 Iterable 等)、基元和枚举不允许作为返回类型。API 参数和返回类型

您可以尝试以下代码来避免Iterable

public static List<Entity> listEntities(String kind) {
    logger.log(Level.INFO, "Search entities based on search criteria");

    Query q = new Query(kind);
    PreparedQuery pq = datastore.prepare(q);
    FetchOptions fetchOptions = FetchOptions.Builder.withDefaults();
    return pq.asList(fetchOptions);
} 
于 2014-03-19T14:58:51.283 回答