今天在写代码的时候,就是用了个ajax去执行,想要获取后台返回的json,但是之前记得在学习的时候后台代码加上@ResponseBody就可以直接返回Json了,但是发现一直在报错!
No converter found for return value of type: class java.util.LinkedHashMap
具体也没搞清楚为啥报这个错,然后发现自己有fastJson的包,但是还是报错,然后测试查百度!发现少了一个包,
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
加上这个包在进行测试发现一切都阳光明媚了
代码如下:
@Controller
@RequestMapping(value = "/user")
public class UserController {
@RequestMapping(value = "/userList")
public String userList(){
return "userList";
}
@RequestMapping(value = "/test")
@ResponseBody
public Map<String,String> test(String str){
Map<String,String> map = new HashMap<String,String>();
map.put("str",str);
return map;
}
}
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<script src="${pageContext.request.contextPath}/js/jquery.js" type="text/javascript"></script>
<script>
function test() {
$.ajax({
type : "POST",
url : "${pageContext.request.contextPath}/user/test.do",
dataType: "json",
data : {
"str" : "123"
},
success : function(data) {
console.log(data);
}
});
}
</script>
</head>
<body>
<input type="button" onclick="test()" value="按钮">
</body>
</html>