你好有神 2015-02-05 03:56 采纳率: 0%
浏览 8709

SpringMVC3.2 无法接收json参数

环境: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()也能使用,但是总觉得不该是这样的,请大家帮忙看下原因,如果问题没有描述清楚的,请提出来,我再改,谢谢了。

  • 写回答

2条回答 默认 最新

  • 你好有神 2015-02-05 08:21
    关注

    目前问题算是解决了,除了问题描述中说的“使用字符串传到后台,然后JSON.parseObject()”,还有一种方法:在方法参数前加@RequestBody,ajax中data:JSON.stringify(formData)。

    评论

报告相同问题?

悬赏问题

  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗