ajax 返回map获取不到 抛出HttpMessageNotWritableException

ajax 返回map获取不到 抛出HttpMessageNotWritableException

  Controller返回String数据,ajax可以正常接收,但是返回对象时(我是map类型),就一直是error。
  如果浏览器打开F12查看网络访问,ajax执行时,Controller的类执行正常,但是返回的map类型数据,ajax获取不到,并抛出异常:HttpMessageNotWritableException

ajax代码:

jQuery.ajax({
	type: "POST",
	url: "updateCustomerMobile.dhtml",
	data: "mobile="+newMobile,
	async: false,
	success: function (msg) {
		alert("成功");
	},
	error: function(){
		alert("失败");
	}
});

Controller代码:

@ResponseBody
@RequestMapping("/updateCustomerMobile.dhtml")
public Map<String, String> updateMoblie(String mobile){
	Map<String, String> map = new HashMap<>();
	...
	return map;	
}

  结果ajax死活弹出"失败",搞得我快崩溃了。

解决方案:

  1. SpringMVC的配置:加上<mvc:annotation-driven />注解扫描
  2. 在pom.xml中加入这两个注解驱动:
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
	<groupId>com.fasterxml.jackson.core</groupId>
	<artifactId>jackson-databind</artifactId>
</dependency>

问题解决,网上的回答乱七八糟的,我在此记录一下,方便大家解决问题。
真的是,阿西吧~

发布了39 篇原创文章 · 获赞 19 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/qq_35394434/article/details/103406445