Java Reference
In-Depth Information
UriBuilder builder =
UriBuilder.fromUri(uriInfo.getRequestUri()).path("{id}");
The uriInfo reference in that expression refers to a UriInfo object injected from the
application context. This is added as an attribute to the implementation:
@Context
private UriInfo uriInfo;
In general, the response from any insert method in a REST application is either “no con-
tent” or the entity itself. Here in the create method I decided to use the entity, because it
includes the generated ID in case the client wants it.
Putting it all together, the create method is as follows:
@POST
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Response create(Person person) {
dao.create(person);
UriBuilder builder =
UriBuilder.fromUri(uriInfo.getRequestUri()).path("{id}");
return Response.created(builder.build(person.getId()))
.entity(person)
.build();
}
The @POST annotation sets the HTTP status code in the response to 201.
The URL patterns for the resource are summarized as follows:
• The base resource pattern is /people . A GET request at that URL returns all the
Person instances. The plural form of Person is used for this reason.
• A POST request at the same URL ( /person ) creates a new Person , assigns it a
URL of its own, and saves it in the database.
• A sub-resource at /people/lastname/{like} uses a URL template (the
like parameter) to do an SQL-like query and find all Person instances who
have a last name satisfying the clause.
• A sub-resource using the URL template {id} supports a GET request that returns
the Person instance with that ID.
Search WWH ::




Custom Search