In Java Enterprise Edition (Java EE), the @FormParam
annotation is used to bind a form parameter to a method parameter in a javax.ws.rs.Consumes
annotated resource class.
If you want to make the form parameter optional, you can use the required
attribute of the @FormParam
annotation and set it to false
. This will allow the method to be called even if the form parameter is not present in the request.
Here's an example of how to use the @FormParam
annotation with the required
attribute set to false
:
import javax.ws.rs.*; @Path("/form") public class FormResource { @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public void submitForm(@FormParam(value = "param1", required = false) String param1, @FormParam("param2") String param2) { // Process the form parameters } }
In this example, the submitForm()
method is annotated with @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
to indicate that it consumes form-encoded data. The param1
and param2
parameters are annotated with the @FormParam
annotation to bind them to form parameters with the same name.
The param1
parameter is marked as optional using the required
attribute, while the param2
parameter is required. This means that the submitForm()
method can be called even if the param1
form parameter is not present in the request, but it must have a param2
form parameter.