HTML forms are an easy means of getting information from a user and they are also easy to create. Form data
can be used for HTTP GET
requests and HTTP POST
requests:
- GET
When form data is sent as part of an HTTP
GET
request the data is appended to the URI as a set of query parameters. Injecting data from query parameters is discussed in Using query parameters.- POST
When form data is sent as part of an HTTP
POST
request the data is placed in the HTTP message body. The form data can be handled using a regular entity parameter that supports the form data. It can also be handled by using the@FormParam
annotation to extract the data and inject the pieces into resource method parameters.
The javax.ws.rs.FormParam
annotation extracts field values from form data and
injects the value into resource method parameters. The annotation takes a single parameter that specifies the
key of the field from which it extracts the values. The associated parameter must conform to the data types
described in Supported data types.
![]() | Important |
---|---|
The JAX-RS API Javadoc states that the |
Example 3.8 shows a resource method that injects form data into its
parameters. The method assumes that the client's form includes three fields—title
,
tags
, and body
—that contain string data.
Example 3.8. Injecting form data into resource method parameters
import javax.ws.rs.FormParam; import javax.ws.rs.POST; ... @POST public boolean updatePost(@FormParam("title") String title, @FormParam("tags") String tags, @FormParam("body") String post) { ... }