Java Reference
In-Depth Information
Working with Forms and URIs
Problem
You want an easy way to get data from an HTML form.
Solution
For each item in the form that you want to capture, use the
@FormParam
annotation within the
method parameters.
Discussion
The form parameters annotation works like many other annotations in JAX-RS, such as
@QueryParam
and
@PathParam
—the values are injected by the runtime. So if you have either
a short form with just a few input controls, or are only interested in a few values, it's very easy
to access form data using
@FormParam
.
in a static representation.
Example8-19.User.java is the business entity the form creates
package com.soacookbook.rest.forms;
import java.util.Date;
/**
* A business entity created by form input.
*/
public class User {
private String id;
private String name;
private Date createdDate;
//getters and setters omitted...
}
In a real-world application, you would use a database or some other persistent store to save
created users. For the purposes of this example, you'll just save them in a static map, as shown
in
Example 8-20
.