Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6920891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:12:22+00:00 2026-05-27T10:12:22+00:00

I want to send a HashMap object to a ReST resource as a POST

  • 0

I want to send a HashMap object to a ReST resource as a POST variable. I used the Form class to send the object. The client code:

public static void main(String[] args)
    {
        ClientConfig config = new DefaultClientConfig();
        Client client = Client.create(config);
        WebResource service = client.resource(getBaseURI());

        HashMap<String, String> hashmap = new HashMap<String, String>();
        hashmap.put("Key1", "value1");
        hashmap.put("Key2", "value2");
        Form form = new Form();

        form.add("data1", hashmap);

        ClientResponse response = service.path("hello2").path("hello2").type(MediaType.APPLICATION_FORM_URLENCODED).post(ClientResponse.class, form);
        @SuppressWarnings("unchecked")
        MultivaluedMap<String, String> map = response.getEntity(MultivaluedMap.class);
        System.out.println(map.get("response").get(0));
        System.out.println(map.get("response2"));
    }

The REST resource is as follows:

@Path("/hello2")
public class FormResource
{
    @Context
    UriInfo uriInfo;
    @Context
    Request request;

    public FormResource()
    {
    }

    public FormResource(UriInfo uriInfo, Request request)// , String data1)
    {
        this.uriInfo = uriInfo;
        this.request = request;
    }

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public MultivaluedMap<String, String> newProj(@FormParam("data1") HashMap<String, String> postVarMap, @Context HttpServletResponse response)
    {

        System.out.println(postVarMap);
        MultivaluedMap<String, String> hash = new MultivaluedMapImpl();
        hash.add("response", "response1");
        hash.add("response", "response2");
        hash.add("response2", "fbshabfsdb");
        URI uri = uriInfo.getAbsolutePathBuilder().build();
        Response.created(uri).build();
        return hash;
    }

}

I get the following exception in the Tomcat 6.0 log

Dec 7, 2011 3:38:39 PM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/JerCDemo] has started
Dec 7, 2011 3:38:40 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  com.reflexis.nbe.jersey
Dec 7, 2011 3:38:40 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class com.reflexis.nbe.jersey.FormResource
Dec 7, 2011 3:38:40 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Dec 7, 2011 3:38:40 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.9.1 09/14/2011 02:05 PM'
Dec 7, 2011 3:38:40 PM com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
  SEVERE: Missing dependency for method public javax.ws.rs.core.MultivaluedMap com.reflexis.nbe.jersey.FormResource.newProj(java.util.HashMap,javax.servlet.http.HttpServletResponse) at parameter at index 0
  SEVERE: Missing dependency for method public javax.ws.rs.core.MultivaluedMap com.reflexis.nbe.jersey.FormResource.newProj(java.util.HashMap,javax.servlet.http.HttpServletResponse) at parameter at index 0
  SEVERE: Method, public javax.ws.rs.core.MultivaluedMap com.reflexis.nbe.jersey.FormResource.newProj(java.util.HashMap,javax.servlet.http.HttpServletResponse), annotated with POST of resource, class com.reflexis.nbe.jersey.FormResource, is not recognized as valid resource method.
Dec 7, 2011 3:38:40 PM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
    at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
    at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:766)
    at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:488)
    at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
    at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4420)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4733)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3460)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1357)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1649)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1658)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1658)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1638)
    at java.lang.Thread.run(Thread.java:619)
Dec 7, 2011 3:38:40 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /JerCDemo threw load() exception
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
    at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
    at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
    at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
    at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:766)
    at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:488)
    at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
    at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
    at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
    at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:556)
    at javax.servlet.GenericServlet.init(GenericServlet.java:212)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1173)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:993)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4420)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4733)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3460)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1357)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1649)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1658)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1658)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1638)
    at java.lang.Thread.run(Thread.java:619)

Is the client code written by me correct?
Is there any other way to send a HashMap object as POST variable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:12:23+00:00Added an answer on May 27, 2026 at 10:12 am

    Just to clear things a bit. The MultivaluedMap<String, String> is meant to be used for obtaining general map of form parameters e.g. parameters submitted to your service via POST HTTP request. It is supposed to be used like this:

       @POST
       @Consumes("application/x-www-form-urlencoded")
       public void post(MultivaluedMap<String, String> formParams) {
         // Store the message
       }
    

    However, when your client application needs to provide your REST service with some sort of data (in your case a HashMap containing I suppose a lot of important information) it would Serialize it to XML first, then send it to the service which would then deserialize and use it. Unfortunately, Jersey is not able to automatically marshal/unmmarshal HashMaps so if you just provided HashMap parameter in your newProj method, you would get an exception.

    So how to send a HashMap to your service? Well, the key is the JAXB @XmlRootElement and a custom XmlAdapter 🙂

    First you need to write your own wrapper for the map. The wrapper will be annotated with @XmlRootElement

    @XmlRootElement
    public class MyHashMapObject<T, U> {
        private Map<T, U> mapProperty;
    
        public MyHashMapObject() {
            mapProperty = new HashMap<T, U>();
        }
    
        @XmlJavaTypeAdapter(MapAdapter.class) // NOTE: Our custom XmlAdaper
        public Map<T, U> getMapProperty() {
            return mapProperty;
        }
    
        public void setMapProperty(Map<T, U> map) {
            this.mapProperty = map;
        }
    }
    

    Then you need to define your “JAXB enabled” map elements:

    public class MapElement {
        @XmlElement
        public String key;
        @XmlElement
        public String value;
    
        private MapElement() {
        }
    
        public MapElement(String key, String value) {
            this.key = key;
            this.value = value;
        }
    }
    

    And in the end define your custom XmlAdapter:

    public class MapAdapter extends XmlAdapter<MapElement[], Map<String, String>> {
        public MapElement[] marshal(Map<String, String> arg0) throws Exception {
            MapElement[] mapElements = new MapElement[arg0.size()];
            int i = 0;
            for (Map.Entry<String, String> entry : arg0.entrySet())
                mapElements[i++] = new MapElement(entry.getKey(), entry.getValue());
    
            return mapElements;
        }
    
        public Map<String, String> unmarshal(MapElement[] arg0) throws Exception {
            Map<String, String> r = new HashMap<String, String>();
            for (MapElement mapelement : arg0)
                r.put(mapelement.key, mapelement.value);
            return r;
        }
    }
    

    Once you have all of this in place (it must be used by your service and the client so put it to some shard jar), you can define your service like this:

    @Path("/hello")
    public class FormResource
    {
        //@GET
        @POST
        @Produces(MediaType.APPLICATION_XML)
        @Consumes(MediaType.APPLICATION_XML)
        public MyHashMapObject<String, String> post(
                    MyHashMapObject<String, String> anotherMap) {
    
            anotherMap.getMapProperty().put("e", "10");
            anotherMap.getMapProperty().put("f", "11");
            anotherMap.getMapProperty().put("g", "12");
    
            return anotherMap;
        }
    }
    

    You’re all set to go now. Your client should go like this:

       public class Test {  
          public static void main(String[] args) {
                ClientConfig config = new DefaultClientConfig();
                Client client = Client.create(config);
                WebResource service = client.resource(getBaseURI());
    
                // Now do the MAP stuff
                MyHashMapObject<String, String> map = new MyHashMapObject<String, String>();
                map.getMapProperty().put("a", "1");
                map.getMapProperty().put("b", "2");
    
                ClientResponse response = service.path("rest").path("hello2")
                    .type(MediaType.APPLICATION_XML)
                    .accept(MediaType.APPLICATION_XML)
                    .post(ClientResponse.class, map);
    
                Map<String, String> myMap = response.getEntity(MyHashMapObject.class).getMapProperty();     
                for(Map.Entry<String, String> entry : myMap.entrySet())
                    System.out.format("Key: %s, Value: %s\n", entry.getKey(), entry.getValue());
    
            }
    
            private static URI getBaseURI() {
                return UriBuilder.fromUri(
                        "http://localhost:8080/org.nowaq.jersey.first").build();
            }
        }
    

    Now you can easily pass your HashMap<String, String> to your REST service. You can also make the implementation a bit more generic. Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to send some strings in a list in a POST call. eg:
I want send a form as soon as the page is loaded. All variables
I created a simple post controller with scaffolding and I want send a mail
I want to send class data through the Scala RemoteActor API. I made a
Can jquery.ajax post a value from an attribute Name? If can, I want send
I have some action. I want send mail. usually i doing this way. public
I have form and some fields and I want send these fields to the
For testing purposes I want send mail to my localhost user account rather than
Want to send the text from my current vb application to the Active Window
I want so send every week an update by email. But Im afraid that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.