环境:SpingMVC3.2+jQuery。
问题:方法能访问到。
1)在ajax中使用contentType : "application/json"时,controller无法获取参数。
2)在ajax中不使用contentType时,controller能获取参数,但是会报错,因为参数中含有list,去掉list可以正常访问。
JS方法片段:
$.ajax({
contentType : "application/json",
url : systemWebUrl + "/pi/savePIDetail.do",
type : "post",
dataType : "json",
//data : {"articleVO":JSON.stringify(formData)},
data : formData,//JSON.stringify(formData),
success : function(data){
console.log(data);
}
});
其中传入后台的参数formData数据类似:
{"nodeId":"8","title":"abc","isOriginal":"1","keywords":"abc","summary":"abc","coverResourceId":"","contents":[{"type":"1","content":"asdf"},{"type":"1","content":"wqer"}]}
Controller代码片段:
@RequestMapping("/savePIDetail")
@ResponseBody
public String savePIDetail(ArticleVO articleVO){
ModelMap map = new ModelMap();
ArticleVO article = new ArticleVO();
//ArticleVO article = JSON.parseObject(articleVO,ArticleVO.class);
map.put("msg", articleVO);
return JSON.toJSONString(map);
}
参数ArticleVO片段:
…………
/**
* 是否原创
*/
private Boolean isOriginal;
/**
* 摘要
*/
private String summary;
/**
* 内容列表
*/
private List<ArticleContentVO> contents;
getter/setter
mvc.xml配置片段:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<ref bean="fastJsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
<value>text/html;charset=UTF-8</value>
<value>text/plain;charset=UTF-8</value>
</list>
</property>
</bean>
我觉得上面这段配置返回时才用得到。
ajax中不使用contentType时,并且参数有list,后台报错内容片段:
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'contents[0][content]' of bean class [com.xxx.admin.vo.ArticleVO]: Property referenced in indexed property path 'contents[0][content]' is neither an array nor a List nor a Map; returned value was [asdf]
折腾了几天了,没找到原因,使用字符串传到后台,然后JSON.parseObject()也能使用,但是总觉得不该是这样的,请大家帮忙看下原因,如果问题没有描述清楚的,请提出来,我再改,谢谢了。