JAX-RS @FormParam annotation example
In this page you will see an example for @FormParam to bind html form fields to your method inputs. It works for http method POST.
In the previous examples we have given details of application setup, dependencies, web.xml file configurations: If you want to
know about these configuration, please refer these:
Restful web services using RESTEasy hello world example.
Restful web services using Jersey hello world example.
Html form:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>User Input Form</title>
</head>
<body>
<h1>Input User Details</h1>
<form action="rest/user-form/register" method="post">
<p>Name : <input type="text" name="name" /></p>
<p>Address : <input type="text" name="address" /></p>
<input type="submit" value="Enter Details" />
</form>
</body>
</html>
|
User registration service:
package com.java2novice.restful;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
@Path("/user-form")
public class UserRegService {
@POST
@Path("/register")
public Response registerUserInfo(@FormParam("name") String name,
@FormParam("address") String address){
String response = "Successfully added user details, name: "+
name+" and address: "+address;
return Response.status(200).entity(response).build();
}
}
|
User Input Form

Output

|