Spring+Vue+ElementUI的前后端分离开发项目中,前端向后端发送请求,后端返回错误:Cannot deserialize instance of `java.util.LinkedHashMap` out of START_ARRAY token
问题描述:
Spring+Vue+ElementUI的前后端分离开发项目中,前端向后端发送请求,后端返回错误:Cannot deserialize instance of `java.util.LinkedHashMap` out of START_ARRAY token。
前端代码:
// 获取分页数据 findPage: function (data) { if(data !== null) { this.pageRequest = data.pageRequest } this.pageRequest.params = [{name:'name', value:this.filters.name}];
this.$api.user.findPage(this.pageRequest).then((res) => { this.pageResult = res.data this.findUserRoles() }).then(data!=null?data.callback:'') }
后端代码:
分页请求参数Java类:
package com.XXXXX.XXXXX.core.page;//包名 import java.util.HashMap; import java.util.Map; /** * 分页请求*/ public class PageRequest { //当前页码 private int pageNum = 1; //每页数量 private int pageSize = 10; //其它查询参数 private Map<String, Object> params = new HashMap<>(); public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public Map<String, Object> getParams() { return params; } public void setParams(Map<String, Object> params) { this.params = params; } public Object getParam(String key) { return getParams().get(key); } }
后端请求控制器类:
package com.XXXXX.XXXXX.admin.controller;//包名 import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.XXXXX.XXXXX.admin.model.SysLoginLog;//导入的包名 import com.XXXXX.XXXXX.admin.service.SysLoginLogService; import com.XXXXX.XXXXX.core.http.HttpResult; import com.XXXXX.XXXXX.core.page.PageRequest; //登录日志控制器 @RestController @RequestMapping("loginlog") public class SysLoginLogController { @Autowired private SysLoginLogService sysLoginLogService; @PreAuthorize("hasAuthority('sys:loginlog:view')") @PostMapping(value="/findPage") public HttpResult findPage(@RequestBody PageRequest pageRequest) { return HttpResult.ok(sysLoginLogService.findPage(pageRequest)); } @PreAuthorize("hasAuthority('sys:loginlog:delete')") @PostMapping(value="/delete") public HttpResult delete(@RequestBody List<SysLoginLog> records) { return HttpResult.ok(sysLoginLogService.delete(records)); } }
解决办法:
经过分析发现,后端java类PageRequest的代码中已经声明params为Map<String, Object>类型,但是前端传入的JSON字符串却是数组类型的,后台无法完成转化,因此将前端的数组删除,改为对象类型。否则,辉抛出Cannot deserialize instance of `java.util.LinkedHashMap` out of START_ARRAY token异常。
经过修改后的前端代码如下所示:
// 获取分页数据 findPage: function (data) { if(data !== null) { this.pageRequest = data.pageRequest } this.pageRequest.params = {name:'name', value:this.filters.name}; this.$api.user.findPage(this.pageRequest).then((res) => { this.pageResult = res.data this.findUserRoles() }).then(data!=null?data.callback:'') }
参考链接:Can not deserialize instance of java.util.LinkedHashMap out of START_ARRAY token