使用fastjson将JsonObject对象转为javaBean对象失败
fastjson
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
项目地址:https://gitcode.com/gh_mirrors/fastj/fastjson
免费下载资源
·
1、前端vue中axios调用后台服务的代码:
onsubmit(){
this.axios.post("/api/leave/leaveApply",{
data:{
type:this.leaveForm.type,
startTime:this.leaveForm.startTime,
endTime:this.leaveForm.endTime,
reason:this.leaveForm.reason,
userId:'user1',
pdId:'myProcess_1:2:15004',
}
}).then((res)=>{
alert(res.data);
}).catch((res)=>{
alert("发起请假失败");
})
}
2、http请求参数如下:
3、springboot后台controller代码如下:
@RequestMapping("/leaveApply")
@ResponseBody
public String leaveApply(@RequestBody String leaveForm){
System.out.println("leaveForm:"+leaveForm);
if(StringUtils.isEmpty(leaveForm)){
return "请假表单数据不允许为空";
}
JSONObject jsonObject=JSON.parseObject(leaveForm);
System.out.println("jsonObj:"+jsonObject);
LeaveForm leaveFormObj=jsonObject.toJavaObject(LeaveForm.class);
System.out.println("leaveFormObj:"+leaveFormObj);
activittiService.startProcess(leaveFormObj.getPdId(),leaveFormObj.getUserId());
return "leaveApply";
}
4、访问controller,后台控制台打印如下信息:
leaveForm:{"data":{"type":"2","startTime":"","endTime":"","reason":"啊","userId":"user1","pdId":"myProcess_1:2:15004"}}
jsonObj:{"data":{"reason":"啊","startTime":"","endTime":"","pdId":"myProcess_1:2:15004","type":"2","userId":"user1"}}
leaveFormObj:LeaveForm{type=null, startTime=null, endTime=null, reason='null', userId='null', pdId='null'}
2018-11-26 14:23:53.545 ERROR 5204 --- [nio-8088-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.activiti.engine.ActivitiIllegalArgumentException: processDefinitionKey and processDefinitionId are null] with root cause
org.activiti.engine.ActivitiIllegalArgumentException: processDefinitionKey and processDefinitionId are null
at org.activiti.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:89) ~[activiti-engine-5.21.0.jar:5.21.0]
at org.activiti.engine.impl.cmd.StartProcessInstanceCmd.execute(StartProcessInstanceCmd.java:37) ~[activiti-engine-5.21.0.jar:5.21.0]
at org.activiti.engine.impl.interceptor.CommandInvoker.execute(CommandInvoker.java:24) ~[activiti-engine-5.21.0.jar:5.21.0]
原因分析:
根据打印的信息可知,json字符串转jsonObject成功了,但jsonObject转javaBean失败了,重点查看jsonObject打印的信息:jsonObj:{"data":{"reason":"啊","startTime":"","endTime":"","pdId":"myProcess_1:2:15004","type":"2","userId":"user1"}}
正常的普通对象是用一对{}包裹的,而这里的格式明显不对,多了一层data{}包裹,由此可见是json字符串的格式有问题,
再返回前台查看传递的数据格式如下:
原来是这里用data包裹数据,在这里修改为下面这样:
再次访问controller,后台打印一下信息:
leaveForm:{"type":"1","startTime":"2018-11-26T06:39:44.560Z","endTime":"2018-11-26T06:39:48.389Z","reason":"啊啊","userId":"user1","pdId":"myProcess_1:2:15004"}
jsonObj:{"reason":"啊啊","startTime":"2018-11-26T06:39:44.560Z","endTime":"2018-11-26T06:39:48.389Z","pdId":"myProcess_1:2:15004","type":"1","userId":"user1"}
leaveFormObj:LeaveForm{type=1, startTime=Mon Nov 26 14:39:44 CST 2018, endTime=Mon Nov 26 14:39:48 CST 2018, reason='啊啊', userId='user1', pdId='myProcess_1:2:15004'}
可以看出,jsonObject(jsonObj)已经没有data{}了,而且也成功转为javaBean(leaveFormObj),问题解决。
GitHub 加速计划 / fastj / fastjson
25.69 K
6.51 K
下载
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
最近提交(Master分支:2 个月前 )
c942c834 - 1 年前
5bc4709b - 1 年前
更多推荐
已为社区贡献6条内容
所有评论(0)