REST& GAE:如何在不使用资源ID的情况下定义getter

时间:2014-12-06 09:13:00

标签: java android google-app-engine

我正在使用带有Cloud Endpoints的Google App Engine来编写简单的API代码。该API只有一个实体:Book,包含字段Long id和String name。

Eclipse的Eclipse插件为我生成了一个API类,它有一个getBook(Long id)方法。但是,我也希望能够得到一本知道它名字的书。也就是说我想要一个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();
}
}

1 个答案:

答案 0 :(得分:0)

不支持重载。你应该只调用你的函数“getBookByName”,“getBookByISBN”等......

相关问题