Spring FormHttpMessageConverter and MultiValueMap

 

FormHttpMessageConverter is an implementation of HttpMessageConverter which can read and write form data from http request and response.

Let's see how to use FormHttpMessageConverter with the help of MultiValueMap for parsing/reading Http Form POST data.

 
Rememberapplication/x-www-form-urlencoded is the default media type. 
 
 
@RequestMapping(value="/enroll",method=RequestMethod.POST, consumes={"text/html","application/x-www-form-urlencoded"})
public void enrollmentSubmission(HttpServletRequest request, HttpServletResponse response,
	@RequestBody MultiValueMap<String,String> parametersMultiMap){
	
	Map<String,String> formParameters = parametersMultiMap.toSingleValueMap();
		
}
 
 
@RequestBody MultiValueMap<String,String> parametersMultiMap : Provides us an easy way for accessing the name-value
parameters submitted through Form. MultiValueMap holds name of the parameter as the key and array of values as key.


If your requirement is strictly single name-value pairs you can convert the MultiValueMap to regular Map using:

Map<String,String> formParameters = parametersMultiMap.toSingleValueMap();


If you want to access the first element of the Value list:
parametersMultiMap.getFirst("EmployeeId")
 
 
Web Analytics