REST Service with MultivaluedMap using Jersey


Here I am going to give an example on how MultivaluedMap in REST web service works. A MultivaluedMap<K, V> is a map of key-values pairs. Each key can have zero or more values, where K – the type of keys maintained by this map and V – the type of mapped values.

The most important concept in REST is resources, which are identified by global IDs — typically using URIs. Client applications use HTTP methods (GET/ POST/ PUT/ DELETE) to manipulate the resource or collection of resources.

A RESTful Web service is implemented using HTTP and the principles of REST. Typically, a RESTful Web service should define the following aspects:


The base/root URI for the Web service such as http://<host>/<appcontext/contextpath>/<url pattern>/<resources>.

MIME Types

The MIME type of the response data supported, which are JSON/XML/TEXT/HTML etc.

HTTP Methods

HTTP methods are mapped to CRUD (create, read, update and delete) actions for a resource. Although you can make slight modifications such as making the PUT method to create or update, the basic patterns are listed as follows.

HTTP GET: Get/List/Retrieve an individual resource or a collection of resources.
HTTP POST: Create a new resource or resources.
HTTP PUT: Update an existing resource or collection of resources.
HTTP DELETE: Delete a resource or collection of resources.


Java 8 or 12, Eclipse 4.12, Jersey API, Gradle 5.6 or Maven 3.6.1

Creating Project

Create maven or gradle based project in Eclipse. The name of the project is rest-jersey-multivalued-map.

For maven based project you can use below information:

Group Id: com.roytuts
Artifact Id: rest-jersey-multivalued-map

Updating Build Script

As the default gradle based project in Eclipse does not have the required dependencies, so we will include required dependencies.

Note you only need jaxb-api if you are using Java 9 or above. If you do not include jaxb-api then you will get an exception: java.lang.NoClassDefFoundError: javax/xml/bind/PropertyException

Make sure you have the following dependencies in your build.gradle file:

plugins {
    id 'java-library'

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {
    implementation ''
    implementation 'javax.servlet:javax.servlet-api:4.0.1'
    implementation 'javax.xml.bind:jaxb-api:2.4.0-b180830.0359'
    implementation 'org.glassfish.jersey.inject:jersey-hk2:2.29.1'
    implementation 'org.glassfish.grizzly:grizzly-http-server:2.4.4'
    implementation 'org.glassfish.jersey.containers:jersey-container-servlet:2.29.1'
    implementation 'org.glassfish.jersey.containers:jersey-container-grizzly2-http:2.29.1'
    testImplementation 'junit:junit:4.12'

Updating Build File

If you are using maven based project then you can use the below pom.xml file.

Note you only need jaxb-api if you are using Java 9 or above. If you do not include jaxb-api then you will get an exception: java.lang.NoClassDefFoundError: javax/xml/bind/PropertyException

<project xmlns="" xmlns:xsi=""

Deployment Descriptor

Though we are going to test our application using Junit test case but ideally your application will be deployed into application server, at least servlet container based server. Therefore you need to have such deployment descriptor – web.xml file.

Modify web.xml file to use the jersey servlet.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi=""
	id="WebApp_ID" version="3.0">

	<display-name>REST multivalued map using Jersey</display-name>


	<!-- Map /rest/* to Jersey -->

REST Resource Class

Now we will create REST resource class. This is a simple class that takes MultivaluedMap as a parameter and sends those as a response in string format.



public class RestResource {

	public String postMultivalued(MultivaluedMap<String, String> multivaluedMap) {
		if (multivaluedMap.get("fname") != null && multivaluedMap.get("lname") != null) {
			String fname = multivaluedMap.get("fname").get(0);
			String lname = multivaluedMap.get("lname").get(0);
			return "Your name : " + fname + " " + lname;
		return "I have not received anything from you";


Write Junit Class

As we said that we are not going to deploy the application into external server. Therefore we will create a Junit class and using Junit class we will deploy the application into Glassfish Grizzly server and test our REST service.




import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class RestResourceTest {

	private HttpServer httpServer;
	private WebTarget webTarget;
	private static final URI baseUri = URI.create("http://localhost:9090/rest/");

	public void setup() throws Exception {
		// create ResourceConfig from Resource class
		ResourceConfig rc = new ResourceConfig(RestResource.class);
		// create the Grizzly server instance
		httpServer = GrizzlyHttpServerFactory.createHttpServer(baseUri, rc);
		// start the server
		// configure client with the base URI path
		Client client = ClientBuilder.newClient();
		webTarget =;

	public void tearDown() throws Exception {
		// if you want to stop the server from the input through keyboard then uncomment
		// below lines

		// System.out.println(String
		// .format("Application started.%nHit enter to stop it..."));
		// stop the server

	public void testPostMultivalued() {
		MultivaluedMap<String, String> map = new MultivaluedHashMap<String, String>();
		map.add("fname", "Soumitra");
		map.add("lname", "Roy");
		String response = webTarget.path("resource/multivalued").request().post(Entity.form(map), String.class);
		System.out.println("Response: " + response);


Testing the Application

Running the above Junit class will give you the below output:

Response: Your name : Soumitra Roy

Source Code


That’s all. Thanks for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *