springMVC接收参数转为bean对象报错

永不消散的Bug 2019-05-17 11:16:29
先贴出错误,通过前台获取form表单的数据,传递到后台,出现如下错误

前台代码
function saveData() {
var data = ncrForm.getData();
$.ajax({
url : context + "ncrContentController/save.do",
contentType : "application/x-www-form-urlencoded",// 此处重点
type : 'POST',
data : data,
dataType : "json",
success : function(result) {
confirmMessage(result, onCancel);
},
error : function(jqXHR, textStatus, errorThrown) {
mini.alert(jqXHR.responseText);
}
});
}

后台controller接收
@RequestMapping("/ncrContentController")
@RestController
public class NcrContentController extends BaseController{

@Autowired
NcrContentService ncrContentService;

@RequestMapping(value = "/save.do", method = RequestMethod.POST)
public BaseResponse save(NcrContent ncrContent) {
BaseResponse baseResponse = new BaseResponse();
try {
//NcrContent ncrContent = JSON.parseObject(ncr, NcrContent.class);
if (StringUtils.isEmpty(ncrContent.getID())) {
ncrContentService.insert(ncrContent);
} else {
ncrContentService.update(ncrContent);
}
return baseResponse;
} catch (Exception e) {
baseResponse.setExceptionMsg(e, "ClassName : " + this.getClass().getName() + " MethodName : "
+ Thread.currentThread().getStackTrace()[1].getMethodName());
return baseResponse;
}
}

}

----------------------------------------------------分割线-------------------------------------------
后来将前台页面改为

function saveData() {
var data = ncrForm.getData();
$.ajax({
url : context + "ncrContentController/save.do",
contentType : "application/json",// 此处重点
type : 'POST',
data : JSON.stringify(data),
dataType : "json",
success : function(result) {
confirmMessage(result, onCancel);
},
error : function(jqXHR, textStatus, errorThrown) {
mini.alert(jqXHR.responseText);
}
});
}

后台改为
@RequestMapping(value = "/save.do", method = RequestMethod.POST)
public BaseResponse save(@RequestBody String ncr) {
BaseResponse baseResponse = new BaseResponse();
try {
NcrContent ncrContent = JSON.parseObject(ncr, NcrContent.class);
if (StringUtils.isEmpty(ncrContent.getID())) {
ncrContentService.insert(ncrContent);
} else {
ncrContentService.update(ncrContent);
}
return baseResponse;
} catch (Exception e) {
baseResponse.setExceptionMsg(e, "ClassName : " + this.getClass().getName() + " MethodName : "
+ Thread.currentThread().getStackTrace()[1].getMethodName());
return baseResponse;
}
}

就可以了。。
但是第一种方法为什么无法接收,直接报错,没有找到原因,望各位大佬帮忙解惑一下
...全文
238 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
softFE 2019-05-17
  • 打赏
  • 举报
回复
contentType 一定要配置吗? 我从来没配置过, 也能用 . 你要不试试 不配置? 或者, 你的页面表单 某些属性有误?
永不消散的Bug 2019-05-17
  • 打赏
  • 举报
回复
引用 1 楼 水边2 的回复:
application/x-www-form-urlencoded 表示传递的参数是 key1=value1&key2=value2 这样的键值对,而不是json格式。 application/json 表示传递的参数是下面这样的json格式: {"key1":"value1","key2":"value2"}
这个我明白,但是controller在接收第一种参数的时候,不是会自动把能匹配上的属性匹配到bean中么,但是我这里就报错了,很奇怪
游北亮 2019-05-17
  • 打赏
  • 举报
回复
application/x-www-form-urlencoded 表示传递的参数是 key1=value1&key2=value2 这样的键值对,而不是json格式。 application/json 表示传递的参数是下面这样的json格式: {"key1":"value1","key2":"value2"}
VERDOM 2019-05-17
  • 打赏
  • 举报
回复
把@RequestBody去掉 或者以application/json格式传参
Nirvana_lss 2019-05-17
  • 打赏
  • 举报
回复
你传的那个data只是一个字符串吧并没有指定它的具体参数名称,所以后台bean获取不到对应的数据
你把data:data这行换成data:{data:data} 试试

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧