Java Reference
In-Depth Information
@Produces("text/html")
public class FormsService {
//no user selected, so present form to create one
@GET
public Response create() {
return Response.ok(NEW_USER_FORM_HTML).build();
}
//get form data and create and save user
@POST
@Consumes("application/x-www-form-urlencoded")
@Produces("text/html")
public Response doCreate(
@FormParam("id") String id,
@FormParam("fname") String fname,
@Context UriInfo uriInfo) {
//add user to database
User user = new User();
user.setId(id);
user.setName(fname);
user.setCreatedDate(new Date());
Database.users.put(id, user);
//create URI here with entity response
URI createdUri =
UriBuilder.fromUri(
uriInfo.getRequestUri().toString())
.path("{a}").build(user.getId());
System.out.println("Created URI: " + createdUri);
//Set the Location header and indicate 201 (Created) response
//Setting the entity in the response returns
//the object that was created, by first running it through
//the writer implementation.
Response response = Response.created(createdUri)
.entity(user)
.location(createdUri)
.build();
return response;
}
Search WWH ::




Custom Search