Cannot deserialize instance of `java.lang.String` out of START_OBJECT token

Cannot deserialize instance of `java.lang.String` out of START_OBJECT token


controller方法:

@PostMapping("/manage/user")
	public ResponseBean createUser(@RequestBody Map map) {
		for (Map.Entry entry:map.entrySet()){
	          System.out.print(" 键:" + entry.getKey());
	          System.out.println("值:" + entry.getValue());
	    }
		return new ResponseBean(200, "Create user successful!", null);
	}

请求内容:

{"roleId":"2","permissionId":"1","user":{"username":"jack","password":"jack"}}

使用Junit测试报错

Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_OBJECT token
 at [Source: (PushbackInputStream); line: 1, column: 41] (through reference chain: java.util.LinkedHashMap["user"])]

经搜索学习,应该是因为请求体中的 user 被认为是一个Object对象,不能用String进行解析,于是修改成

@PostMapping("/manage/user")
	public ResponseBean createUser(@RequestBody Map map) {
		for (Map.Entry entry:map.entrySet()){
	          System.out.print(" 键:" + entry.getKey());
	          System.out.println("值:" + entry.getValue());
	    }
		return new ResponseBean(200, "Create user successful!", null);
	}

再运行,就成功了。
参考链接:https://www.e-learn.cn/index.php/content/wangluowenzhang/313898

你可能感兴趣的:(Java)