Java Reference
In-Depth Information
Braces vs. Brackets
Groovy uses curly braces for closures, so the literal notation to define a Java array should
use square brackets for a java.util .ArrayList instead.
The complete PersonResource implementation in Groovy is shown in the next listing.
Listing 9.6. A Groovy implementation of the PersonResource class
@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()
}
Search WWH ::




Custom Search