Skip to main content

Jersey (JAX-RS) @FormParam HTML form data handling

There are multiple ways for consuming HTML form data (application/x-www-form-urlencoded) in Jersey. Using @FormParam annotation we can inject Form values in the Resource method. We can use it just like other @*Param.

Jersey resource method needs to know they have to handle HTML form data, for it we explicitly specify @Consumes("application/x-www-form-urlencoded").


There are multiple ways in which we can handle HTML form data using Jersey. Injecting Form data using @FormParam is one of them.

Use @FormParam

Using @FormParam we can inject specific HTML form parameters values in the Resource method. Its use is similar to other @*Param annotations.

File: FormParamResource.java

package in.geekmj.resource;

import javax.ws.rs.Consumes;
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.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.springframework.stereotype.Component;

/*
 * 
 * @author geekmj Three ways to get Form data in Jersey
 */
@Path("/form-data")
@Produces(MediaType.TEXT\_HTML)
@Component
public class FormParamResource {

	/*
	 * Using @FormParam inject form data in method arguments
	 * 
	 * @param name
	 * @param phoneNumber
	 * @return
	 */
	@POST
	@Path("/form-param")
	@Consumes(MediaType.APPLICATION\_FORM\_URLENCODED)
	public Response getFormDataUsingFormParam(@FormParam("name") String name, @FormParam("phone") String phoneNumber) {
		
		return Response.ok(getHtmlResponse(name, phoneNumber)).build();
	}

	private String getHtmlResponse(String name, String phoneNumber) {
		StringBuilder responseStr = new StringBuilder("<html><head><title>Form Data</title></head><body>");
		responseStr.append("<h2>Submitted form data</h2><div><span>Name : "+name+"</span><br/><span>Phone : "+phoneNumber+"</span></div></body>");
		return responseStr.toString();
	}
}

We have used @FormParam to get names and phone from x-www-form-urlencoded form-data request.

Use MultivaluedMap in method injection

We can inject all Form values in the method as MultivaluedMap<String,String> when method has consume("application/x-www-form-urlencoded") annotation.

Note: It may not work in Spring Boot when the Spring Web module is used. You may see a warning in the log saying A servlet request to the URI https://xyz.com/abc contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.

File: FormParamResource.java

package in.geekmj.resource;

import javax.ws.rs.Consumes;
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.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.springframework.stereotype.Component;

/*
 * 
 * @author geekmj Three ways to get Form data in Jersey
*/
@Path("/form-data")
@Produces(MediaType.TEXT\_HTML)
@Component
public class FormParamResource {

	/*
	 * Using Multivalued Map inject form data in method single map argument
	 * Note: It may not work if another Servlet is processing the request before Jersey Servlet or filter
	 * @param name
	 * @param phoneNumber
	 * @return
	 */
	@POST
	@Path("/multivalued-map")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response getFormDataUsingMultivaluedMap(MultivaluedMap<String, String> formParams) {
		
		//Same as formValues.get("name").get(0);
		String name = formParams.getFirst("name");
		String phoneNumber = formParams.getFirst("phone");
		return Response.ok(getHtmlResponse(name, phoneNumber)).build();
	}

	private String getHtmlResponse(String name, String phoneNumber) {
		StringBuilder responseStr = new StringBuilder("<html><head><title>Form Data</title></head><body>");
		responseStr.append("<h2>Submitted form data</h2><div><span>Name : "+name+"</span><br/><span>Phone : "+phoneNumber+"</span></div></body>");
		return responseStr.toString();
	}
}

Use UriInfo method injection with @Context

We can get all form values in MultivaluedMap<string, string> using UriInfo.getQueryParameters() method. UriInfo can injected as method attribute using @Context annotation.

Note: It may not work in Spring Boot when Spring Web module used.

package in.geekmj.resource;

import javax.ws.rs.Consumes;
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.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

import org.springframework.stereotype.Component;

/*
 * 
 * @author geekmj Three ways to get Form data in Jersey
*/
@Path("/form-data")
@Produces(MediaType.TEXT\_HTML)
@Component
public class FormParamResource {

	/*
	 * Using Multivalued Map inject form data in method single map argument
	 * Note: It may not work if another Servlet is processing the request before Jersey Servlet or filter
	 * @param name
	 * @param phoneNumber
	 * @return
	*/
	@POST
	@Path("/context")
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public Response getFormDataUsingContext(@Context UriInfo ui) {
		
		MultivaluedMap<String, String> formValues = ui.getQueryParameters();
		
		//Same as formValues.get("name").get(0);
		String name = formValues.getFirst("name");
		String phoneNumber = formValues.getFirst("phone");
		return Response.ok(getHtmlResponse(name, phoneNumber)).build();
	}

	private String getHtmlResponse(String name, String phoneNumber) {
		StringBuilder responseStr = new StringBuilder("<html><head><title>Form Data</title></head><body>");
		responseStr.append("<h2>Submitted form data</h2><div><span>Name : "+name+"</span><br/><span>Phone : "+phoneNumber+"</span></div></body>");
		return responseStr.toString();
	}
}

Testing form data handling by Jersey

File: testform.html

<!DOCTYPE html>
<html>
	<head>
	<meta charset="UTF-8">
	<title>Form to test Jersey form data handling</title>
	</head>
<body>

	<h2>Jersey form data handling using @formParam annotation</h2>
	<form action="/form-data/form-param" method="POST" name="form1">
		<label>Name</label> <input maxlength="15" name="name"/> 
		<br/><br/>
		<label>Phone</label> <input maxlength="15" name="phone"/> 
		<br/><br/>
		<input type="submit" value="Submit" />
		<input type="reset" value="Reset" />
	</form>
	<br/>
	<h2>Jersey form data handling using MultivaluedMap injected</h2>
	<form action="/form-data/multivalued-map" method="POST" name="form2">
		<label>Name</label> <input maxlength="15" name="name" /> 
		<br/><br/>
		<label>Phone</label> <input maxlength="15" name="phone"/> 
		<br/><br/>
		<input type="submit" value="Submit" />
		<input type="reset" value="Reset" />
	</form>
	<br/>
	<h2>Jersey form data handling using context.getQueryParameters() </h2>
	<form action="/form-data/context" method="POST" name="form3">
		<label>Name</label> <input maxlength="15" name="name"/> 
		<br/><br/>
		<label>Phone</label> <input maxlength="15" name="phone"/> 
		<br/><br/>
		<input type="submit" value="Submit" />
		<input type="reset" value="Reset" />
	</form>
</body>
</html>

Download the complete source code for this project and then follow this instruction to run the application.

Go to https://localhost:8080/testform.html.

Jersey form data handling test

References

  1. Official Jersey Documentation

  2. Download the Full Project

  3. Follow Project On Github

Comments

Popular posts from this blog

Working with request header in Jersey (JAX-RS) guide

In the  previous post , we talked about, how to get parameters and their values from the request query string. In this guide learn how to get request header values in Jersey (JAX-RS) based application. We had tested or used the following tools and technologies in this project: Jersey (v 2.21) Gradle Build System (v 2.9) Spring Boot (v 1.3) Java (v 1.8) Eclipse IDE This is a part of  Jersey (JAX-RS) Restful Web Services Development Guides series. Please read Jersey + Spring Boot getting started guide . Gradle Build File We are using Gradle for our build and dependency management (Using Maven rather than Gradle is a very trivial task). File: build.gradle buildscript { ext { springBootVersion = '1.3.0.RELEASE' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' a

Ajax Cross Domain Resource Access Using jQuery

Some time back in our project we faced a problem while making an Ajax call using jQuery. Chrome Browser console had given some weird error message like below when we try to access one of our web pages: When we try to access the same web page in the Firefox browser, it doesn't give any error in the console but some parsing error occurred. In our case, we were accessing XML as an Ajax request resource. I was curious to check if the non-XML cross-domain resource was successfully loading or not. But finally, I realized that it is not going through. jersey-spring-boot-quick-starter-guide In our Ajax call, requesting domain was not the same as the requested URL domain. $.ajax({ url: "https://10.11.2.171:81/xxxxxx/xxxxxxx.xml" , type : "get" , success: function (response) { alert( "Load was performed." ); }, error : function (xhr, status) {

FastAPI first shot

Setup on my Mac (Macbook Pro 15 inch Retina, Mid 2014) Prerequisite Python 3.6+ (I used 3.7.x. I recently reinstalled OS after cleaning up disk, where stock Python 2.7 was available. I installed Pyenv and then used it to install 3.7.x). I already had a git repo initialized at Github for this project. I checked that out. I use this approach to keep all the source code safe or at a specific place 😀. I set the Python version in .python-version file. I also initialize the virtual environment using pyenv in venv folder. I started the virtual environment. FastAPI specific dependencies setup Now I started with basic pip commands to install dependency for the project. I saved dependencies in requirements.txt  the file. Minimal viable code to spin an API Server FastAPI is as cool as NodeJS or Go Lang (?) to demonstrate the ability to spin an API endpoint up and running in no time. I had the same feeling for the Flask too, which was also super cool. app/main.py: from typing i