1

我正在玩GAE Cloud端點和數據存儲區,遵循開發人員網站上的所有示例。我得到一個錯誤生成客戶端庫時,我解決不了:GAE雲端點錯誤生成客戶端庫 - java.lang.IllegalArgumentException

「在線程異常‘主’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

請嘗試將這些進口java.lang.iterable,com.google.common.collect.Iterables您util.java

+0

當我導入com.google.common.collect.Iterables時,出現導入錯誤:導入com.google.common無法解析 – user2556560

+0

好吧,鏈接了guava-libraries,並解析了導入,但得到的是完全相同的像以前一樣錯誤。 – user2556560

+0

也許洞錯誤堆棧會有幫助。這裏是: – user2556560

0

參數化類型,如可迭代等。,原語和枚舉不允許作爲返回類型。 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); 
} 
0

端點返回類型應該是JavaBean的。 注意:不支持返回簡單類型,如String或int。返回值需要是JavaBean(非參數化),數組或集合。 CollectionResponse(com.google.api.server.spi.response.CollectionResponse)或其子類具有特殊待遇。目前不支持參數化bean。

相關問題