document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package com.apuntesdejava.rest;
  2.  
  3. import com.apuntesdejava.rest.domain.Persona;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6. import java.util.logging.Logger;
  7. import javax.ws.rs.Consumes;
  8. import javax.ws.rs.GET;
  9. import javax.ws.rs.POST;
  10. import javax.ws.rs.Path;
  11. import javax.ws.rs.PathParam;
  12. import javax.ws.rs.Produces;
  13. import javax.ws.rs.core.MediaType;
  14.  
  15. @Path("/personas")
  16. public class PersonaResource {
  17.  
  18.     static int contador = 0;
  19.     static final Logger LOGGER = Logger.getLogger(PersonaResource.class.getName());
  20.     static final List<Persona> lista = new ArrayList<>();
  21.  
  22.     @POST
  23.     @Consumes(MediaType.APPLICATION_JSON)
  24.     public void crear(Persona p) {
  25.         p.setId(++contador);
  26.         lista.add(p);
  27.     }
  28.  
  29.     @GET
  30.     @Produces(MediaType.APPLICATION_JSON)
  31.     @Path("/{id}")
  32.     public Persona getPersonaById(@PathParam("id") int id) {
  33.         for (Persona persona : lista) {
  34.             if (persona.getId() == id) {
  35.                 return persona;
  36.             }
  37.         }
  38.         return null;
  39.     }
  40.    
  41.     @GET
  42.     @Produces(MediaType.APPLICATION_JSON)
  43.     public List<Persona> getPersonas() {
  44.         return lista;
  45.     }
  46. }
');