package dk.thoerup.webservice; import java.util.Collection; import java.util.GregorianCalendar; import java.util.HashMap; import javax.validation.constraints.NotNull; import javax.validation.constraints.Size; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; @Path("/persons") public class PersonService { HashMap persons = new HashMap(); public PersonService() { persons.put(1, new Person(1, "Anders And", "Paradisæblevej 113", new GregorianCalendar(1956,10,13).getTime() )); persons.put(2, new Person(2, "Onkel Joakim", "Pengetanken 1", new GregorianCalendar(1966,10,1).getTime() )); persons.put(3, new Person(3, "Fætter Højben", "Firkløvervej 7", new GregorianCalendar(1967,7,7).getTime() )); } /** * http://localhost:8080/Test4Simple/rest/persons/list * @return */ @Path("/list") @GET @Produces(MediaType.APPLICATION_JSON) public Collection getList() { return persons.values(); } /** * http://localhost:8080/Test4Simple/rest/persons/get/2 * @return */ @Path("/get/{id}") @GET @Produces(MediaType.APPLICATION_JSON) public Person getById(@PathParam("id") int id) { Person p = persons.get(id); if (p == null) throw new PersonNotFoundException(); return p; } /** * http://localhost:8080/Test4Simple/rest/persons/search?q=anders * @return */ @Path("/search") @GET @Produces(MediaType.APPLICATION_JSON) public Person search( @Size(min=1) @QueryParam("q") String q ) { q = q.toLowerCase(); for(Person p : persons.values()) { if (p.getName().toLowerCase().contains(q)) return p; } throw new PersonNotFoundException(); } }