-
Notifications
You must be signed in to change notification settings - Fork 1
/
PersonResource.groovy
64 lines (56 loc) · 1.82 KB
/
PersonResource.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path
import javax.ws.rs.PathParam
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
@Path('/people')
class PersonResource {
@Context
private UriInfo uriInfo
PersonDAO dao = JdbcPersonDAO.instance
@GET @Produces( [MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML] )
List<Person> findAll() {
dao.findAll();
}
@GET @Path("lastname/{like}")
@Produces([MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML])
List<Person> findByName(@PathParam("like") String like) {
dao.findByLastName(like);
}
@GET @Path("{id}")
@Produces([MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML])
Response findById(@PathParam("id") long id) {
Response.ok(dao.findById(id))
.build()
}
@POST
@Consumes([MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML])
@Produces([MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML])
Response create(Person person) {
dao.create(person);
UriBuilder builder = UriBuilder.fromUri(uriInfo.requestUri).path("{id}")
Response.created(builder.build(person.id))
.entity(person)
.build()
}
@PUT @Path("{id}")
@Consumes([MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML])
@Produces([MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML])
Person update(Person person) {
dao.update(person);
person;
}
@DELETE @Path("{id}")
Response remove(@PathParam("id") long id) {
dao.delete(id);
Response.noContent().build()
}
}