springMVC接收异步请求时contentType : 'application/json'的作用

$.ajax({
                 url : addUrl,
                 type : 'POST',
                 data : JSON.stringify(productCategoryList),
                 contentType : 'application/json',
                 success : function(data) {
                      if (data.success) {
                            $.toast('提交成功!');
                            getList();
                      } else {
                            $.toast('提交失败!');
                      }
                 }

这是在普通的json提交中(无表单无文件)使用springmvc接收的异步请求,其中contentType: “application/json”的作用是当添加这一句说明时,传入的data必须是json的字符串形式(JSON.stringify(json对象)),而不加这一句说明时可以直接传入json对象,但是重要的是无论是哪种方法在controller层的形参前都必须加上@RequestBody这一注解,否则会报错(例如用List集合接收不加注解报错org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface] with root cause)

你可能感兴趣的:(java,EE,框架相关)