/[projects]/miscJava/Test4Simple/src/main/java/dk/thoerup/webservice/PersonService.java
ViewVC logotype

Contents of /miscJava/Test4Simple/src/main/java/dk/thoerup/webservice/PersonService.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2522 - (show annotations) (download)
Wed Apr 29 09:47:11 2015 UTC (9 years ago) by torben
File size: 2029 byte(s)
Improve PersonSearch
1 package dk.thoerup.webservice;
2
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.GregorianCalendar;
6 import java.util.HashMap;
7
8 import javax.validation.constraints.NotNull;
9 import javax.validation.constraints.Size;
10 import javax.ws.rs.GET;
11 import javax.ws.rs.Path;
12 import javax.ws.rs.PathParam;
13 import javax.ws.rs.Produces;
14 import javax.ws.rs.QueryParam;
15 import javax.ws.rs.core.MediaType;
16
17
18 @Path("/persons")
19 public class PersonService {
20
21 HashMap<Integer,Person> persons = new HashMap<Integer,Person>();
22
23 public PersonService() {
24 persons.put(1, new Person(1, "Anders And", "Paradisæblevej 113", new GregorianCalendar(1956,10,13).getTime() ));
25 persons.put(2, new Person(2, "Onkel Joakim", "Pengetanken 1", new GregorianCalendar(1966,10,1).getTime() ));
26 persons.put(3, new Person(3, "Fætter Højben", "Firkløvervej 7", new GregorianCalendar(1967,7,7).getTime() ));
27 }
28
29
30 /**
31 * http://localhost:8080/Test4Simple/rest/persons/list
32 * @return
33 */
34 @Path("/list")
35 @GET
36 @Produces(MediaType.APPLICATION_JSON)
37 public Collection<Person> getList() {
38 return persons.values();
39 }
40
41
42 /**
43 * http://localhost:8080/Test4Simple/rest/persons/get/2
44 * @return
45 */
46 @Path("/get/{id}")
47 @GET
48 @Produces(MediaType.APPLICATION_JSON)
49 public Person getById(@PathParam("id") int id) {
50 Person p = persons.get(id);
51 if (p == null)
52 throw new PersonNotFoundException();
53
54 return p;
55 }
56
57
58
59 /**
60 * http://localhost:8080/Test4Simple/rest/persons/search?q=anders
61 * @return
62 */
63 @Path("/search")
64 @GET
65 @Produces(MediaType.APPLICATION_JSON)
66 public Collection<Person> search(
67 @QueryParam("q") String q
68 ) {
69 q = q.toLowerCase();
70
71 ArrayList<Person> result = new ArrayList<Person>();
72
73 for(Person p : persons.values()) {
74 if (p.getName().toLowerCase().contains(q)) {
75 result.add(p);
76 }
77
78 }
79 if (result.isEmpty()) {
80 throw new PersonNotFoundException();
81 }
82
83 return result;
84 }
85
86
87 }

  ViewVC Help
Powered by ViewVC 1.1.20