@RequestMapping(value="/quick10",produces = {"text/html;charset=utf-8"})
@ResponseBody
//期望SpringMVC自动将User转换成json格式的字符串
public User save10(){
User user = new User();
user.setName("绝区零");
user.setAge(18);
return user;
}
正确写法应该是:
@RequestMapping(value="/quick10",produces = {"application/json;charset=utf-8"})
@ResponseBody
//期望SpringMVC自动将User转换成json格式的字符串
public User save10(){
User user = new User();
user.setName("绝区零");
user.setAge(18);
return user;
}
错误原因:由于方法是引用@ResponseBody注解,表示返回的是字符串,而此时要把@RequestMapping中的produces的值改为application/json才不会报错,如果是text/html则表示跳转到某个页面,前端无法解析为JSON字符串。


![No converter for [class com.duan.domain.User] with preset Content-Type ‘null‘ No converter for [class com.duan.domain.User] with preset Content-Type ‘null‘](http://www.mshxw.com/aiimages/31/1035656.png)
