Ajax 将“映射”对象传递给 Spring MVC 控制器

Posted

技术标签:

【中文标题】Ajax 将“映射”对象传递给 Spring MVC 控制器【英文标题】:Ajax pass a "Map" object to Spring MVC Controller 【发布时间】:2013-08-17 02:21:04 【问题描述】:

Spring MVC 似乎不知道如何将 javascript“地图”映射到 Java 地图对象

在 Web UI 中,比如 foo.jsp,

<script>
var myMap = ;
myMap["people"] = ["Alex","Bob","Charles","Dave"];
myMap["fruit"]  = ["Apple","Orange"];
$.ajax(
         type : "POST",
         url :  "/myURL",
         data : "myMap=" + myMap, // I tried "myMap="+JSON.stringify(myMap),  as well, it doesn't work neither
         success : function(response) 
             alert("Success! response =  " + response);
         ,
         error : function(e) 
             alert("AJAX error");
         
);
</script>

在服务器端,我有一个数据模型类,只是为了从 Web UI 接收数据

@Setter @Getter
class Parameters 
    private Map<String, List<String>> myMap; //this is the java class I want to map the string to

在控制器中,

@RequestMapping(value = "/myURL", method = RequestMethod.POST)
@ResponseBody
public List<String> fooControl(Parameters parameters ) 
   // do something with parameters ...

我在服务器端遇到的错误是这样的,

[tomcat:launch] Aug 14, 2013 3:12:37 PM org.apache.catalina.core.StandardWrapperValve invoke
[tomcat:launch] SEVERE: Servlet.service() for servlet dispatcher threw exception
[tomcat:launch] org.springframework.validation.BindException:   
org.springframework.validation.BeanPropertyBindingResult: 1 errors  
[tomcat:launch] Field error in object 'Parameters ' on field 
'myMap': rejected value ["people":["Alex","Bob","Charles","Dave"],"fruit":    
["Apple","Orange"]]; codes     
[typeMismatch.repairInfomationParametersExperimental.constraints,typeMismatch.constraints,typeMismatch.java.util.Map,typeMismatch]; arguments  
[org.springframework.context.support.DefaultMessageSourceResolvable: codes 
[repairInfomationParametersExperimental.constraints,constraints]; arguments []; default message 
[constraints]]; default message [Failed to convert property value of type 'java.lang.String' to 
required type 'java.util.Map' for property 'constraints'; nested exception is 
java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type 
[java.util.Map] for property 'myMap': no matching editors or conversion strategy found]

我想有一种方法可以告诉 Spring 如何将 JSON 格式字符串映射到 Java Map?

谢谢!

【问题讨论】:

【参考方案1】:

我已使用以下代码将 Map 对象传递给 Java:

Javascript 代码:

var values = 
                    "object1" : JSON.stringify(object1),
                    "object2" : JSON.stringify(object2)
            ;
 var response = $http.post(url,data);

服务器端代码:

@RequestMapping(value = "/deleteData",method = RequestMethod.POST,consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Result deleteData(@RequestBody HashMap<String, Object> dataHashMap) 
    Object1 object1=  (Object1) JsonConvertor.jsonToObject((String) dataHashMap.get("object1"), Object1.class);
        Object2 object2= (Object2) JsonConvertor.jsonToObject((String) dataHashMap.get("object2"), Object2.class);   

【讨论】:

【参考方案2】:

修改javascript代码:

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

修改服务器端java代码:

@RequestMapping(value = "/myURL", method = RequestMethod.POST, consumes="application/json")
@ResponseBody
public List<String> fooControl(@RequestBody Map<String, List<String>> myMap) 
   // do something with parameters ...

【讨论】:

以上是关于Ajax 将“映射”对象传递给 Spring MVC 控制器的主要内容,如果未能解决你的问题,请参考以下文章

java spring boot将字符串数组作为json中的参数传递给ajax调用

通过jquery ajax将json对象传递给webmethod

如何在 Wordpress 中使用 Ajax 将 Javascript 对象传递给 PHP

使用 jQuery Ajax 将单个对象传递给 MVC 控制器方法

Javascript ajax 将动态创建的 json 对象传递给 Web 服务

如何将对象上下文传递给 jQuery.ajax JSONP 回调?