Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. @Path("Pet")
  2. public class PetResource {
  3.  
  4. @Context
  5. private UriInfo context;
  6.  
  7. public PetResource() {
  8. }
  9.  
  10. @GET
  11. @Produces(MediaType.APPLICATION_JSON)
  12. public Response getJson() {
  13. PetMapper pm = new PetMapper();
  14. JSONConverter jsonconv = new JSONConverter();
  15. List<Pet> petList = pm.getPets();
  16. String json = jsonconv.getJsonFromPets(petList);
  17. return Response.ok().entity(json).build();
  18. }
  19.  
  20. @PUT
  21. @Path("/size")
  22. @Consumes(MediaType.APPLICATION_JSON)
  23. public Response getPetSize() {
  24. PetMapper pm = new PetMapper();
  25. return Response.ok(pm.getPetSize()).build();
  26. }
  27. }
  28.  
  29. public class JSONConverter {
  30.  
  31. static Gson gson = new GsonBuilder().setPrettyPrinting().create();
  32.  
  33. public String getJsonFromPets(List<Pet> pets) {
  34. String petsString = "";
  35. for (Pet pet : pets) {
  36. petsString += gson.toJson(pet) + " ";
  37. }
  38. return petsString;
  39. }
  40. }
  41.  
  42. {
  43. "id": 1,
  44. "name": "Fiddo",
  45. "birth": "2015-02-01",
  46. "species": "Dog",
  47. "owner_id": 1,
  48. "events": []
  49. } {
  50. "id": 2,
  51. "name": "Hannibal",
  52. "birth": "2013-05-10",
  53. "species": "Dog",
  54. "owner_id": 1,
  55. "events": []
  56. } {
  57. "id": 3,
  58. "name": "Elvis",
  59. "birth": "2010-08-08",
  60. "species": "Cat",
  61. "owner_id": 3,
  62. "events": []
  63. } {
  64. "id": 4,
  65. "name": "Sam",
  66. "birth": "2012-01-05",
  67. "species": "Rabbit",
  68. "death": "2015-07-07",
  69. "owner_id": 2,
  70. "events": []
  71. }
  72.  
  73. public String getJsonFromPets(List<Pet> pets) {
  74. return gson.toJson(pets);
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement