5

I've a Java Script map having key value pairs which i Need to send to spring boot contoller :--

Example :--

 var myMap = new Map();
     myMap.set('1', 'value1');
     myMap.set('2', 'value2');
    

I'm not able to get this JavaScript (js) map in my spring boot controller. To my best practice I'm trying to get this map in java's HashMap or Map. A help is highly appreciated. :)

1
  • convert it to JSON and post it through AJAX.. Commented May 24, 2018 at 12:29

3 Answers 3

2

Try below:

var myMap = {};
myMap["names"] = ["Alex"];
myMap["fruit"]  = ["Apple"];

Modify javascript code to use Ajax:

$.ajax({
     type : "POST",
     url :  "/reqURL",
     contentType: "application/json",
     data : JSON.stringify(myMap) // .....

Controller code like below:

 @RequestMapping(value = "/reqURL", method = RequestMethod.POST, consumes="application/json")
    @ResponseBody
    public List<String> reqControl(@RequestBody Map<String, List<String>> myMap) {
       // do something with parameters ...
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot Amit For Responding, The above Suggestion is absolutely correct However, JSON.stringify isn't working as it is Printing -- "{}" on browser's console and on spring controller ( "{}") .
How {} is coming, JSON structure need to be enclosed with {}, share a sample JSON, is Spring Controller complaining and not able to convert to aMap?
This solution does/will not work!! Maps are handled differently.
0

I've figured out the solution :-

var myMap = new Map();
         myMap.set('1', 'value1');
         myMap.set('2', 'value2');
         myMap.set('3', 'value3');
         myMap.set('4', 'value4');

         //Making JS Map compatible for JSON.Stringify
         const out = Object.create(null)
         myMap.forEach((value, key) => {
          if (value instanceof Map) {
            out[key] = map_to_object(value)

          }
          else {
            out[key] = value
          }
        })

$.ajax({
     type : "POST",
     url :  "/yourURL",
     contentType: "application/json",
     data : JSON.stringify(myMap) //......

@RequestMapping(value = "/yourURL", method = RequestMethod.POST, consumes="application/json")
    @ResponseBody
    public List<String> reqControl(@RequestBody Map<String,<String> myMap) {
       //  further code.   
    }

Comments

0

In my case I pass the map to a servlet but maybe it can help someone:


    $.ajax({
    ...
    data: { 
     "MY_MAP": JSON.stringify(Object.fromEntries(myMap))
    },
    ...

String myMapString = parameters.get("MY_MAP");
Map<String, String> myMap = new Gson().fromJson(myMapString, new TypeToken<HashMap<String, String>>() {}.getType());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.