Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Path("Pet")
- public class PetResource {
- @Context
- private UriInfo context;
- public PetResource() {
- }
- @GET
- @Produces(MediaType.APPLICATION_JSON)
- public Response getJson() {
- PetMapper pm = new PetMapper();
- JSONConverter jsonconv = new JSONConverter();
- List<Pet> petList = pm.getPets();
- String json = jsonconv.getJsonFromPets(petList);
- return Response.ok().entity(json).build();
- }
- @PUT
- @Path("/size")
- @Consumes(MediaType.APPLICATION_JSON)
- public Response getPetSize() {
- PetMapper pm = new PetMapper();
- return Response.ok(pm.getPetSize()).build();
- }
- }
- public class JSONConverter {
- static Gson gson = new GsonBuilder().setPrettyPrinting().create();
- public String getJsonFromPets(List<Pet> pets) {
- String petsString = "";
- for (Pet pet : pets) {
- petsString += gson.toJson(pet) + " ";
- }
- return petsString;
- }
- }
- {
- "id": 1,
- "name": "Fiddo",
- "birth": "2015-02-01",
- "species": "Dog",
- "owner_id": 1,
- "events": []
- } {
- "id": 2,
- "name": "Hannibal",
- "birth": "2013-05-10",
- "species": "Dog",
- "owner_id": 1,
- "events": []
- } {
- "id": 3,
- "name": "Elvis",
- "birth": "2010-08-08",
- "species": "Cat",
- "owner_id": 3,
- "events": []
- } {
- "id": 4,
- "name": "Sam",
- "birth": "2012-01-05",
- "species": "Rabbit",
- "death": "2015-07-07",
- "owner_id": 2,
- "events": []
- }
- public String getJsonFromPets(List<Pet> pets) {
- return gson.toJson(pets);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement