RESTfu­­l Jav­a­ wit­h ­JAX­-­­RS 2.­0­ (Second Edition)


The annotation is used to access application/x-www-form-urlencoded request bodies. In other words, it’s used to access individual entries posted by an HTML form document. For example, let’s say we set up a form on our website to register new customers:

<FORM action="" method="post">
    First name: <INPUT type="text" name="firstname"><BR>
    Last name: <INPUT type="text" name="lastname"><BR>
    <INPUT type="submit" value="Send">

We could post this form directly to a JAX-RS backend service described as follows:

public class CustomerResource {

   public void createCustomer(@FormParam("firstname") String first,
                               @FormParam("lastname") String last) {

Here, we are injecting firstname and lastname from the HTML form into the Java parameters first and last. Form data is URL-encoded when it goes across the wire. When using @FormParam, JAX-RS will automatically decode the form entry’s value before injecting it.