/[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 2521 - (show annotations) (download)
Wed Apr 29 09:32:47 2015 UTC (9 years ago) by torben
File size: 1872 byte(s)
Add Person REST service

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

  ViewVC Help
Powered by ViewVC 1.1.20