2013-03-01 61 views
2

我一直試圖在生成的端點類中創建一些新方法,並且發現這種奇怪的行爲:我可以向生成的類添加方法,但是我無法添加其中的兩個,無論我添加哪兩個。 這是我生成的類,在這裏我加了兩個補充方法的代碼的代碼:無法在Google雲端點的端點類中創建更多方法

package it.raffaele.bills; 

import java.util.HashMap; 
import java.util.LinkedList; 
import java.util.List; 

import javax.annotation.Nullable; 
import javax.inject.Named; 
import javax.jdo.PersistenceManager; 
import javax.jdo.Query; 
import javax.persistence.EntityExistsException; 
import javax.persistence.EntityNotFoundException; 

import com.google.api.server.spi.config.Api; 
import com.google.api.server.spi.response.CollectionResponse; 
import com.google.appengine.api.datastore.Cursor; 
import com.google.appengine.datanucleus.query.JDOCursorHelper; 

@Api(name = "utenteendpoint") 
public class UtenteEndpoint { 

    /** 
    * This method lists all the entities inserted in datastore. 
    * It uses HTTP GET method and paging support. 
    * 
    * @return A CollectionResponse class containing the list of all entities 
    * persisted and a cursor to the next page. 
    */ 
    @SuppressWarnings({ "unchecked", "unused" }) 
    public CollectionResponse<Utente> listUtente(
      @Nullable @Named("cursor") String cursorString, 
      @Nullable @Named("limit") Integer limit) { 

     PersistenceManager mgr = null; 
     Cursor cursor = null; 
     List<Utente> execute = null; 

     try { 
      mgr = getPersistenceManager(); 
      Query query = mgr.newQuery(Utente.class); 
      if (cursorString != null && cursorString != "") { 
       cursor = Cursor.fromWebSafeString(cursorString); 
       HashMap<String, Object> extensionMap = new HashMap<String, Object>(); 
       extensionMap.put(JDOCursorHelper.CURSOR_EXTENSION, cursor); 
       query.setExtensions(extensionMap); 
      } 

      if (limit != null) { 
       query.setRange(0, limit); 
      } 

      execute = (List<Utente>) query.execute(); 
      cursor = JDOCursorHelper.getCursor(execute); 
      if (cursor != null) 
       cursorString = cursor.toWebSafeString(); 

      // Tight loop for fetching all entities from datastore and accomodate 
      // for lazy fetch. 
      for (Utente obj : execute) 
       ; 
     } finally { 
      mgr.close(); 
     } 

     return CollectionResponse.<Utente> builder().setItems(execute) 
       .setNextPageToken(cursorString).build(); 
    } 

    /** 
    * This method gets the entity having primary key id. It uses HTTP GET method. 
    * 
    * @param id the primary key of the java bean. 
    * @return The entity with primary key id. 
    */ 
    public Utente getUtente(@Named("id") Long id) { 
     PersistenceManager mgr = getPersistenceManager(); 
     Utente utente = null; 
     try { 
      utente = mgr.getObjectById(Utente.class, id); 
     } finally { 
      mgr.close(); 
     } 
     return utente; 
    } 

    /** 
    * This inserts a new entity into App Engine datastore. If the entity already 
    * exists in the datastore, an exception is thrown. 
    * It uses HTTP POST method. 
    * 
    * @param utente the entity to be inserted. 
    * @return The inserted entity. 
    */ 
    public Utente insertUtente(Utente utente) { 
     PersistenceManager mgr = getPersistenceManager(); 
     try { 
      if (containsUtente(utente)) { 
       throw new EntityExistsException("Object already exists"); 
      } 
      mgr.makePersistent(utente); 
     } finally { 
      mgr.close(); 
     } 
     return utente; 
    } 

    /** 
    * This method is used for updating an existing entity. If the entity does not 
    * exist in the datastore, an exception is thrown. 
    * It uses HTTP PUT method. 
    * 
    * @param utente the entity to be updated. 
    * @return The updated entity. 
    */ 
    public Utente updateUtente(Utente utente) { 
     PersistenceManager mgr = getPersistenceManager(); 
     try { 
      if (!containsUtente(utente)) { 
       throw new EntityNotFoundException("Object does not exist"); 
      } 
      mgr.makePersistent(utente); 
     } finally { 
      mgr.close(); 
     } 
     return utente; 
    } 

    /** 
    * This method removes the entity with primary key id. 
    * It uses HTTP DELETE method. 
    * 
    * @param id the primary key of the entity to be deleted. 
    * @return The deleted entity. 
    */ 
    public Utente removeUtente(@Named("id") Long id) { 
     PersistenceManager mgr = getPersistenceManager(); 
     Utente utente = null; 
     try { 
      utente = mgr.getObjectById(Utente.class, id); 
      mgr.deletePersistent(utente); 
     } finally { 
      mgr.close(); 
     } 
     return utente; 
    } 


/********************************ADDED CODE*********************************************/ 

    @SuppressWarnings({"cast", "unchecked"}) 
     public List<Bill> getUserBillsByTag(@Named("tag") String tag){ 
     PersistenceManager mgr = getPersistenceManager(); 
      Utente utente = null; 
      List<Bill> list = new LinkedList<Bill>(); 
      try { 
       Query q = mgr.newQuery(Utente.class); 
       q.setFilter("nfcID == '" + tag +"'"); 
       List<Utente> utenti = (List<Utente>) q.execute(); 
       if (!utenti.isEmpty()){ 
        for (Utente u : utenti){ 
         list.addAll(u.getBollettini()); 
         break; //fake loop. 
        } 

       }else{ 
        //handle error 

       } 


      } finally { 
       mgr.close(); 
      } 
      return list; 


     } 


    @SuppressWarnings({"cast", "unchecked"}) 
    public List<Bill> getUserBills(@Named("id") Long id){ 
     Utente utente = getUtente(id); 
     System.out.println(utente); 
     List<Bill> list = utente.getBollettini(); 
     return list; 
    } 


/*******************************************************************************/  


    private boolean containsUtente(Utente utente) { 
     PersistenceManager mgr = getPersistenceManager(); 
     boolean contains = true; 
     try { 
      mgr.getObjectById(Utente.class, utente.getId()); 
     } catch (javax.jdo.JDOObjectNotFoundException ex) { 
      contains = false; 
     } finally { 
      mgr.close(); 
     } 
     return contains; 
    } 

    private static PersistenceManager getPersistenceManager() { 
     return PMF.get().getPersistenceManager(); 
    } 

} 

你知道怎麼幫我?我錯過了什麼嗎?

+0

你說「不能添加」,但你能告訴我到底發生了什麼? – 2013-03-01 23:34:18

+0

是的確定:如果我添加另一個自定義方法,端點客戶端庫的生成失敗。鑑於當前版本的API返回沒有詳細的錯誤,幾乎不可能理解爲什麼。 – Raffo 2013-03-03 20:49:13

回答

7

您的方法具有相同的api描述(path="utenteendpoint/{param}")。 給他們一個不同的路徑:

@ApiMethod(path="utenteendpoint/tag/{tag}/") 
public List<Bill> getUserBillsByTag(@Named("tag") String tag) { ... } 

@ApiMethod(path="utenteendpoint/user/{id}/") 
public List<Bill> getUserBills(@Named("id") Long id) { ... } 
+0

你引用的方法是私人的,它不影響上面的代碼。首先,它是由Google工具包本身生成的,正如我的問題所述,在我標記的代碼部分中,我提到了兩種方法。只需添加其中一個就可以使編譯過程成功。加兩者都不是。 – Raffo 2013-03-05 11:42:29

+0

對不起,我改變了我的答案。 – Nipper 2013-03-06 16:21:54

相關問題