Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Api(name = "birra")
- In line 1 above we use the @Api attribute.
- This attribute tells App Engine to expose this class as a REST\RPC endpoints.
- Be aware that all the public methods on this class will be accessible via REST endpoint.
- I have also changed the name to birra to match with the rest of the application.
- We will later see this name show up the REST URL.
- public class BeerEndpoint {
- /**
- * This method lists all the entities inserted in datastore.
- * It uses HTTP GET method.
- *
- * @return List of all entities persisted.
- */
- @SuppressWarnings({"cast", "unchecked"})
- public List<Beer> listBeer() {
- PersistenceManager mgr = getPersistenceManager();
- List<Beer> result = null;
- try{
- Query query = mgr.newQuery(Beer.class);
- result = (List<Beer>) query.execute();
- // Tight loop for fetching all entities from datastore and accommodate
- // for lazy fetch.
- for (Beer beer:result);
- } finally {
- mgr.close();
- }
- return result;
- }
- In 2-24, GPE has defined a basic list function.
- It simply returns all the Beers in the datastore.
- This method will be exposed as a http GET on method
- /**
- * 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 Beer getBeer(@Named("id") Long id) {
- PersistenceManager mgr = getPersistenceManager();
- Beer beer = null;
- try {
- beer = mgr.getObjectById(Beer.class, id);
- } finally {
- mgr.close();
- }
- return beer;
- }
- In the above section, GPE has given us a basic get method.
- Given an ID for a beer, it will return the full beer instance.
- It is exposed as an HTTP GET.. for example beer/42
- /**
- * This inserts the entity into App Engine datastore.
- * It uses HTTP POST method.
- *
- * @param beer the entity to be inserted.
- * @return The inserted entity.
- */
- public Beer insertBeer(Beer beer) {
- PersistenceManager mgr = getPersistenceManager();
- try {
- mgr.makePersistent(beer);
- } finally {
- mgr.close();
- }
- return beer;
- }
- In the above section, GPE has given us a basic insert method.
- It takes the Beer and stores it in the datastore, which gives it
- an ID, then we return it back to the client.
- It is exposed as an HTTP POST.
- /**
- * This method is used for updating a entity.
- * It uses HTTP PUT method.
- *
- * @param beer the entity to be updated.
- * @return The updated entity.
- */
- public Beer updateBeer(Beer beer) {
- PersistenceManager mgr = getPersistenceManager();
- try {
- mgr.makePersistent(beer);
- } finally {
- mgr.close();
- }
- return beer;
- }
- In the above section, GPE has given us a basic update method.
- It takes the Beer updates it in the database.
- It is exposed as an HTTP PUT.
- /**
- * 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 Beer removeBeer(@Named("id") Long id) {
- PersistenceManager mgr = getPersistenceManager();
- Beer beer = null;
- try {
- beer = mgr.getObjectById(Beer.class, id);
- mgr.deletePersistent(beer);
- } finally {
- mgr.close();
- }
- return beer;
- }
- In the above section, GPE has given us a basic delete method.
- It takes an ID of a beer and removes it from database.
- It is exposed as an HTTP DELETE.
- private static PersistenceManager getPersistenceManager() {
- return PMF.get().getPersistenceManager();
- }
- }
Add Comment
Please, Sign In to add comment