记录错误Can not deserialize instance of java.lang.String out of START_ARRAY token

记录一下遇到的错误。

2018-12-12 12:40:23.158  WARN 3128 --- [nio-8090-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not deserialize instance of java.lang.String out of START_ARRAY token
 at [Source: java.io.PushbackInputStream@7eac1ba3; line: 1, column: 61] (through reference chain: java.util.LinkedHashMap["userids"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of java.lang.String out of START_ARRAY token
 at [Source: java.io.PushbackInputStream@7eac1ba3; line: 1, column: 61] (through reference chain: java.util.LinkedHashMap["userids"])

后端代码:

@RequestMapping(value="/assignexperts",method=RequestMethod.POST)
	public Map assignexperts(@RequestBody Map map){
		/*String result= userService.queryprojlist(map);
		 Map resultmap = new HashMap();
		 resultmap.put("result", result);
		String  param= JSON.toJSONString(resultmap);
		return param;*/
		return userService.assignexperts(map);
	}

前端代码:

function assign(){
	  var webRootPath = '<%=request.getContextPath()%>';	
	  var ruleIds=new Array();
	 // var ruleIds = {};
	var i=0;
	  var proj_id=$("#input").val();
		$("#expertShowTable .js-ck input[name='messagePush']:checked").each(function(){
			/*ruleIds[i] = {};
			ruleIds[i]['userid'] =$(this).val();
			i++;*/
			ruleIds.push($(this).val());
		});
		console.log(ruleIds);
		var qaram = {
				"proj_id" : proj_id,
			"userids": ruleIds
			
			};
		console.log( JSON.stringify(qaram));
		
		$.postJSON(webRootPath + "/assignexperts", qaram, function(data) {
              if (data) {
				
				alert(data.result);
				
				
			}else{
				alert("操作失败!");
			}
		});
  }

这里的操作的选择多行数据进行操作,
记录错误Can not deserialize instance of java.lang.String out of START_ARRAY token_第1张图片
提交的时候$.postJSON()方法会把前端传入的var qaram = {
“proj_id” : proj_id,
“userids”: ruleIds

		};

转化成JSON格式到后端,这里前端传入的值是

记录错误Can not deserialize instance of java.lang.String out of START_ARRAY token_第2张图片
由于 var ruleIds=new Array();,这里的userids的值是个数组,所以
这样的形式其实是Map,而在后端的接口中写的是
public Map assignexperts(@RequestBody Map map)
所以会报错说什么String Array 不能序列化成map,要注意前后端参数的类型一定要一致,之前没有意识到这种写法的类型其实是Map,一直用的 Map,导致很久没有找到错误原因

你可能感兴趣的:(记录错误Can not deserialize instance of java.lang.String out of START_ARRAY token)