本文介绍了如何将JavaScript(js)映射传递给Spring Boot Controller?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java Script映射,其中包含我需要发送给spring boot contoller的键值对:-

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

示例:-

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

我无法在Spring Boot控制器中获取此JavaScript(js)映射.按照我的最佳实践,我试图在Java的HashMap或Map中获取此地图.非常感谢您的帮助. :)

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. :)

推荐答案

我已经找到了解决方案:-

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.
    }

这篇关于如何将JavaScript(js)映射传递给Spring Boot Controller?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:47