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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<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>
<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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!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>
<!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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<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><!--Ad Injection:random-->
<div style=''><center><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- javainsimpleway_ad2 -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9157887158094484"
     data-ad-slot="2945253859"
     data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></center></div>
        
                    <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>
<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><!--Ad Injection:random-->
<div style=''><center><script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- javainsimpleway_ad2 -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-9157887158094484"
     data-ad-slot="2945253859"
     data-ad-format="auto"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script></center></div>
		
                    <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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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();
        
    }
}
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

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