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.


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


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" 
  <name>FormParam Maven Webapp</name>

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" >
  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>
  17.     <servlet-mapping>
  18.         <servlet-name>jersey-serlvet</servlet-name>
  19.         <url-pattern>/rest/*</url-pattern>
  20.     </servlet-mapping>
  21. </web-app>
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

  <display-name>Archetype Created Web Application</display-name>


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>
  5.     <form action="http://localhost:8080/FormParam/rest/user/create" method="post">
  6.             <p>
  7.             Name : <input type="text" name="name" />
  8.             </p><!--Ad Injection:random-->
  9. <div style=''><center><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
  10. <!-- post_random_1 -->
  11. <ins class="adsbygoogle"
  12.      style="display:block; text-align:center;"
  13.      data-ad-layout="in-article"
  14.      data-ad-format="fluid"
  15.      data-ad-client="ca-pub-9157887158094484"
  16.      data-ad-slot="5005839043"></ins>
  17. <script>
  18.      (adsbygoogle = window.adsbygoogle || []).push({});
  19. </script></center></div>
  21.                     <p>
  22.             State : <input type="text" name="state" />
  23.             </p>
  25.                     <p>
  26.             Age : <input type="text" name="age" />
  27.             </p>
  29.                     <p>
  30.             Skills : Java : <input type="checkbox" name="skill" value="Java"/>
  31.             Spring : <input type="checkbox" name="skill" value="Spring" />
  32.             </p>
  33.         <input type="submit" value="Register" />
  34.     </form>
  36. </body>
  37. </html>
	<h1>JAX-RS @FormQuery Example</h1>

	<form action="http://localhost:8080/FormParam/rest/user/create" method="post">
			Name : <input type="text" name="name" />
		    </p><!--Ad Injection:random-->
<div style=''><center><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- post_random_1 -->
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     (adsbygoogle = window.adsbygoogle || []).push({});
			State : <input type="text" name="state" />
			Age : <input type="text" name="age" />
			Skills : Java : <input type="checkbox" name="skill" value="Java"/> 
			Spring : <input type="checkbox" name="skill" value="Spring" />
		<input type="submit" value="Register" />


Step 4

Create the rest service class to use @FormParam as below

  1. package com.kb.rest;
  3. import java.util.List;
  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;
  11. @Path("/user")
  12. public class FormParamUserService {
  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){
  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 ";
  24.         return Response.status(200).entity(output).build();
  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;

public class FormParamUserService {

	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


We will get below Output once we click on Register


Download this project JAXRSFormParam.zip

About the Author

Karibasappa G C (KB)
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