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
Step 1
Update pom.xml with below dependencies
- <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
- <!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
- <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>
<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
- 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
We will get below Output once we click on Register
It is not working