JAX-RS @FormParam with example


What is @FormParam ?

This annotation is used to bind the form values to method variables just like @QueryParam but @QueryParam will work only if client makes a GET request.

If client makes a POST request, then all the parameters submitted by the POST request can be accessed using @FormParam at the method parameters level.

Note

Use @QueryParam if it’s a GET request and use @FormParam if it’s a POST request.

Let’s see it through an example


Create a new Maven Web project in eclipse (Refer Rest service Hello World project for the same)

Project structure


form_param_proj_structure

Step 1

Update pom.xml with below dependencies

  1. <project xmlns="http://maven.apache.org/POM/4.0.0"
  2.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  4.         http://maven.apache.org/maven-v4_0_0.xsd">
  5.   <modelVersion>4.0.0</modelVersion>
  6.   <groupId>FormParam</groupId>
  7.   <artifactId>FormParam</artifactId>
  8.   <packaging>war</packaging>
  9.   <version>0.0.1-SNAPSHOT</version>
  10.   <name>FormParam Maven Webapp</name>
  11.   <url>http://maven.apache.org</url>
  12.   <dependencies>
  13.     <dependency>
  14.       <groupId>junit</groupId>
  15.       <artifactId>junit</artifactId>
  16.       <version>3.8.1</version>
  17.       <scope>test</scope>
  18.     </dependency>
  19.      <dependency>
  20.     <groupId>org.glassfish.jersey.containers</groupId>
  21.     <artifactId>jersey-container-servlet</artifactId>
  22.     <version>2.24</version>
  23. </dependency>
  24.   </dependencies>
  25.   <build>
  26.     <finalName>FormParam</finalName>
  27.   </build>
  28. </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
         http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>FormParam</groupId>
  <artifactId>FormParam</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>FormParam Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
     <dependency>
    <groupId>org.glassfish.jersey.containers</groupId>
    <artifactId>jersey-container-servlet</artifactId>
    <version>2.24</version>
</dependency>
  </dependencies>
  <build>
    <finalName>FormParam</finalName>
  </build>
</project>

We have added dependencies for Jersey container servlet and Junit in the above pom file.

Step 2

Update web.xml file with Jersey servlet container

  1. <!DOCTYPE web-app PUBLIC
  2.  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3.  "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4.  
  5. <web-app>
  6.   <display-name>Archetype Created Web Application</display-name>
  7.    <servlet>
  8.         <servlet-name>jersey-serlvet</servlet-name>
  9.          <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  10.         <init-param>
  11.             <param-name>jersey.config.server.provider.packages</param-name>
  12.             <param-value>com.kb.rest</param-value>
  13.         </init-param>
  14.         <load-on-startup>1</load-on-startup>
  15.     </servlet>
  16.  
  17.     <servlet-mapping>
  18.         <servlet-name>jersey-serlvet</servlet-name>
  19.         <url-pattern>/rest/*</url-pattern>
  20.     </servlet-mapping>
  21. </web-app>
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
   <servlet>
		<servlet-name>jersey-serlvet</servlet-name>
		 <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
		<init-param>
			<param-name>jersey.config.server.provider.packages</param-name>
			<param-value>com.kb.rest</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>jersey-serlvet</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
</web-app>

we have defined a special servlet called “jersey-serlvet” in web.xml and mapped it by the URL pattern /rest/*

Step 3

Create the HTML page to make POST request as below

  1. <html>
  2. <body>
  3.     <h1>JAX-RS @FormQuery Example</h1>
  4.  
  5.     <form action="http://localhost:8080/FormParam/rest/user/create" method="post">
  6.             <p>
  7.             Name : <input type="text" name="name" />
  8.             </p>
  9.        
  10.                     <p>
  11.             State : <input type="text" name="state" />
  12.             </p>
  13.        
  14.                     <p>
  15.             Age : <input type="text" name="age" />
  16.             </p>
  17.        
  18.                     <p>
  19.             Skills : Java : <input type="checkbox" name="skill" value="Java"/>
  20.             Spring : <input type="checkbox" name="skill" value="Spring" />
  21.             </p>
  22.         <input type="submit" value="Register" />
  23.     </form>
  24.  
  25. </body>
  26. </html>
<html>
<body>
	<h1>JAX-RS @FormQuery Example</h1>

	<form action="http://localhost:8080/FormParam/rest/user/create" method="post">
		    <p>
			Name : <input type="text" name="name" />
		    </p>
		
                    <p>
			State : <input type="text" name="state" />
		    </p>
		
                    <p>
			Age : <input type="text" name="age" />
		    </p>
		
                    <p>
			Skills : Java : <input type="checkbox" name="skill" value="Java"/> 
			Spring : <input type="checkbox" name="skill" value="Spring" />
		    </p>
		<input type="submit" value="Register" />
	</form>

</body>
</html>


Step 4

Create the rest service class to use @FormParam as below

  1. package com.kb.rest;
  2.  
  3. import java.util.List;
  4.  
  5. import javax.ws.rs.FormParam;
  6. import javax.ws.rs.POST;
  7. import javax.ws.rs.Path;
  8. import javax.ws.rs.Produces;
  9. import javax.ws.rs.core.Response;
  10.  
  11. @Path("/user")
  12. public class FormParamUserService {
  13.  
  14.     @POST
  15.     @Path("/create")
  16.     @Produces("text/html")
  17.     public Response getUserDetails(@FormParam("name") String name,@FormParam("state") String state,
  18.                                        @FormParam("age") String age,@FormParam("skill") List<String> skills){
  19.            
  20.         //Service call to save Data in DB
  21.         String output =name+" from the state "+state+ " with the age "+age+ "and with the skills "
  22.                                                                                             +skills+" is created ";
  23.        
  24.         return Response.status(200).entity(output).build();
  25.        
  26.     }
  27. }
package com.kb.rest;

import java.util.List;

import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;

@Path("/user")
public class FormParamUserService {

	@POST
	@Path("/create")
	@Produces("text/html")
	public Response getUserDetails(@FormParam("name") String name,@FormParam("state") String state,
                                       @FormParam("age") String age,@FormParam("skill") List<String> skills){
			
		//Service call to save Data in DB
		String output =name+" from the state "+state+ " with the age "+age+ "and with the skills "
                                                                                            +skills+" is created ";
		
        return Response.status(200).entity(output).build();
		
	}
}

We have used @FormParam at the method argument level to receive the values submitted in the form.

Step 5

Build the project and deploy in server

Step 6

Lets see the output

Open client.html in web browser

Enter the details and click on Register

output_1

We will get below Output once we click on Register

output_2

Download this project JAXRSFormParam.zip

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on