我正在使用开发者网站上的所有示例来使用GAE Cloud端点和数据存储区。我在生成客户端库时遇到了无法解决的错误:
“线程中的异常”main“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);
}
}
答案 0 :(得分:0)
请尝试将这些导入java.lang.iterable,com.google.common.collect.Iterables添加到您的util.java
答案 1 :(得分:0)
不允许使用Iterable ect。,Primitives和enums等参数化类型作为返回类型。 API Parameter and Return Types
您可以尝试使用以下代码来避免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);
}
答案 2 :(得分:0)
端点返回类型应该是JavaBean。 注意:不支持返回诸如String或int之类的简单类型。返回值必须是JavaBean(非参数化),数组或集合。 CollectionResponse(com.google.api.server.spi.response.CollectionResponse)或其子类有特殊处理。通常,目前不支持参数化bean。