2014-12-06 2 views
0

간단한 API를 코딩하기 위해 Cloud Endpoints와 함께 Google App Engine을 사용하고 있습니다. 이 API에는 Long ID 및 String 이름 필드가있는 Book이라는 엔티티 하나만 있습니다.REST & GAE : 리소스 ID를 사용하지 않고 getter를 정의하는 방법

Eclipse 용 Google 플러그인은 getBook (Long ID) 메소드가있는 API 클래스를 생성했습니다. 그러나, 나는 또한 그 이름을 아는 책을 얻을 수 있기를 원합니다. 즉, getBookByName (String name) 메소드도 갖고 싶습니다. 나에게 간단한 코드 나 그와 비슷한 것을 보여주는 링크를 보여 주시겠습니까? 쿼리 개체와 함께 JDO 프레임 워크를 사용해야한다고 생각합니다. 여기

는 API 클래스 코드 :

@Api(name = "bookendpoint") 
public class BookEndpoint { 
/** 
* 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" }) 
@ApiMethod(name = "listBook") 
public CollectionResponse<Book> listBook(
     @Nullable @Named("cursor") String cursorString, 
     @Nullable @Named("limit") Integer limit) { 

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

    try { 
     mgr = getPersistenceManager(); 
     Query query = mgr.newQuery(Book.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<Book>) 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 (Book obj : execute) 
      ; 
    } finally { 
     mgr.close(); 
    } 

    return CollectionResponse.<Book> 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. 
*/ 
@ApiMethod(name = "getBook") 
public Book getBook(@Named("id") Long id) { 
    PersistenceManager mgr = getPersistenceManager(); 
    Book book = null; 
    try { 
     book = mgr.getObjectById(Book.class, id); 
    } finally { 
     mgr.close(); 
    } 
    return book; 
} 
/** 
* 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 book the entity to be inserted. 
* @return The inserted entity. 
*/ 
@ApiMethod(name = "insertBook") 
public Book insertBook(Book book) { 
    PersistenceManager mgr = getPersistenceManager(); 
    try { 
     if (book.getId() != null) { 
     if (containsBook(book)) { 
      throw new EntityExistsException("Object already exists"); 
     } 
     } 
     mgr.makePersistent(book); 
    } finally { 
     mgr.close(); 
    } 
    return book; 
} 

/** 
* 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 book the entity to be updated. 
* @return The updated entity. 
*/ 
@ApiMethod(name = "updateBook") 
public Book updateBook(Book book) { 
    PersistenceManager mgr = getPersistenceManager(); 
    try { 
     if (!containsBook(book)) { 
      throw new EntityNotFoundException("Object does not exist"); 
     } 
     mgr.makePersistent(book); 
    } finally { 
     mgr.close(); 
    } 
    return book; 
} 

/** 
* 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. 
*/ 
@ApiMethod(name = "removeBook") 
public void removeBook(@Named("id") Long id) { 
    PersistenceManager mgr = getPersistenceManager(); 
    try { 
     Book book = mgr.getObjectById(Book.class, id); 
     mgr.deletePersistent(book); 
    } finally { 
     mgr.close(); 
    } 
} 

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

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

게시물 해당 API 클래스 코드를 참조하십시오. –

+0

내 질문을 편집했습니다. – Gannicus

답변

0

오버로드가 지원되지 않습니다. "getBookByName", "getBookByISBN"등의 함수를 호출해야합니다.

+0

네, 그렇게 보입니다. 그러나 나는 그 이름만을 아는 책을 검색하기위한 간단한 코드를 요구했다. – Gannicus

+0

내가 뭔가를 놓친 건가요? 숨겨진 비밀 메소드가 없다. 이름 속성에 대한 질의가 필요한 것처럼 들린다. JPA 프레임 워크에서 쿼리를 작성하는 방법 인 JPQL을 살펴보십시오. [1] [1] - http://www.objectdb.com/java/jpa/query/jpql/structure – Nick

+0

예 이름 속성에 대해서만 쿼리하면됩니다. 그러나 JPA가 아니라 JDO를 사용하고 있습니다. – Gannicus

관련 문제