Guest User

Untitled

a guest
Nov 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. @Api(name = "birra")
  2.  
  3. In line 1 above we use the @Api attribute.
  4. This attribute tells App Engine to expose this class as a REST\RPC endpoints.
  5. Be aware that all the public methods on this class will be accessible via REST endpoint.
  6.  
  7. I have also changed the name to birra to match with the rest of the application.
  8. We will later see this name show up the REST URL.
  9.  
  10. public class BeerEndpoint {
  11.  
  12. /**
  13. * This method lists all the entities inserted in datastore.
  14. * It uses HTTP GET method.
  15. *
  16. * @return List of all entities persisted.
  17. */
  18. @SuppressWarnings({"cast", "unchecked"})
  19. public List<Beer> listBeer() {
  20. PersistenceManager mgr = getPersistenceManager();
  21. List<Beer> result = null;
  22. try{
  23. Query query = mgr.newQuery(Beer.class);
  24. result = (List<Beer>) query.execute();
  25. // Tight loop for fetching all entities from datastore and accommodate
  26. // for lazy fetch.
  27. for (Beer beer:result);
  28. } finally {
  29. mgr.close();
  30. }
  31. return result;
  32. }
  33.  
  34. In 2-24, GPE has defined a basic list function.
  35. It simply returns all the Beers in the datastore.
  36. This method will be exposed as a http GET on method
  37.  
  38. /**
  39. * This method gets the entity having primary key id.
  40. * It uses HTTP GET method.
  41. *
  42. * @param id the primary key of the java bean.
  43. * @return The entity with primary key id.
  44. */
  45. public Beer getBeer(@Named("id") Long id) {
  46. PersistenceManager mgr = getPersistenceManager();
  47. Beer beer = null;
  48. try {
  49. beer = mgr.getObjectById(Beer.class, id);
  50. } finally {
  51. mgr.close();
  52. }
  53. return beer;
  54. }
  55.  
  56.  
  57. In the above section, GPE has given us a basic get method.
  58. Given an ID for a beer, it will return the full beer instance.
  59. It is exposed as an HTTP GET.. for example beer/42
  60. /**
  61. * This inserts the entity into App Engine datastore.
  62. * It uses HTTP POST method.
  63. *
  64. * @param beer the entity to be inserted.
  65. * @return The inserted entity.
  66. */
  67. public Beer insertBeer(Beer beer) {
  68. PersistenceManager mgr = getPersistenceManager();
  69. try {
  70. mgr.makePersistent(beer);
  71. } finally {
  72. mgr.close();
  73. }
  74. return beer;
  75. }
  76.  
  77.  
  78. In the above section, GPE has given us a basic insert method.
  79. It takes the Beer and stores it in the datastore, which gives it
  80. an ID, then we return it back to the client.
  81. It is exposed as an HTTP POST.
  82.  
  83.  
  84. /**
  85. * This method is used for updating a entity.
  86. * It uses HTTP PUT method.
  87. *
  88. * @param beer the entity to be updated.
  89. * @return The updated entity.
  90. */
  91. public Beer updateBeer(Beer beer) {
  92. PersistenceManager mgr = getPersistenceManager();
  93. try {
  94. mgr.makePersistent(beer);
  95. } finally {
  96. mgr.close();
  97. }
  98. return beer;
  99. }
  100.  
  101.  
  102. In the above section, GPE has given us a basic update method.
  103. It takes the Beer updates it in the database.
  104. It is exposed as an HTTP PUT.
  105. /**
  106. * This method removes the entity with primary key id.
  107. * It uses HTTP DELETE method.
  108. *
  109. * @param id the primary key of the entity to be deleted.
  110. * @return The deleted entity.
  111. */
  112. public Beer removeBeer(@Named("id") Long id) {
  113. PersistenceManager mgr = getPersistenceManager();
  114. Beer beer = null;
  115. try {
  116. beer = mgr.getObjectById(Beer.class, id);
  117. mgr.deletePersistent(beer);
  118. } finally {
  119. mgr.close();
  120. }
  121. return beer;
  122. }
  123.  
  124. In the above section, GPE has given us a basic delete method.
  125. It takes an ID of a beer and removes it from database.
  126. It is exposed as an HTTP DELETE.
  127.  
  128. private static PersistenceManager getPersistenceManager() {
  129. return PMF.get().getPersistenceManager();
  130. }
  131.  
  132. }
Add Comment
Please, Sign In to add comment