[英]GAE - Getter without Setter - How to prevent client from writing a given property? How to prevent client from modifying the object ID?
[英]REST&GAE: How to define a getter without using resource Id
我正在將Google App Engine與Cloud Endpoints結合使用來編寫簡單的API。 該API只有一個實體:Book,其字段為Long id和String name。
Google 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();
}
}
不支持重載。 您應該只調用函數“ getBookByName”,“ getBookByISBN”等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.