关于SpringMVC返回Json数据报错:Content type 'application/json;charset=UTF-8' not support

appleYQL 2017-05-16 04:46:04
今天用springMVC返回json数据是报错:Content type 'application/json;charset=UTF-8' not support,以下是代码
springMVC的方法是:
@RequestMapping(value="/requestJson.action",method=RequestMethod.POST)
@ResponseBody
public Student requestJson(@RequestBody Student student){
return student;
}

Ajax的请求数据方式是使用jquery的:
function requestJson() {
$.ajax({
type:'post',
url:'/TestAjax/Ajax/requestJson.action',
contentType:'application/json;charset=utf-8',
dataType:'json',
//数据格式是json串
data:'{"name":"木头人"}',
success:function(data){ //测试能不能返回数据
alert(data);
}
});
}

搜过这个问题,有人说是有属性没有get方法引起的,但是我将bean注释只剩下一个属性了:
package com.testAjax.pojo;

public class Student {

//private String id;

private String name;

//private int age;

//private Date date;



/*public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}



public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Date getDate() {
return date;
}

public void setDate(Date date) {
this.date = date;
}*/

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

另外,楼主试过了,如果只是接受字符串数据,springMVC没有报错,即这样:
@RequestMapping(value="/requestJson.action",method=RequestMethod.POST)
@ResponseBody
public String requestJson(@RequestBody String name){


return name;

}

日志是显示接收成功的:
DEBUG [http-nio-8080-exec-4] - Read [class java.lang.String] as "application/json;charset=UTF-8" with [org.springframework.http.converter.StringHttpMessageConverter@779952f5]
DEBUG [http-nio-8080-exec-4] - Written [{"name":"木头人"}] as "application/json" using [org.springframework.http.converter.StringHttpMessageConverter@779952f5]
DEBUG [http-nio-8080-exec-4] - Null ModelAndView returned to DispatcherServlet with name 'springMVC': assuming HandlerAdapter completed request handling
DEBUG [http-nio-8080-exec-4] - Successfully completed request

求救,这种错误应该怎么改,各位大神帮帮在下,分数奉上!!!
...全文
4311 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
叫我小傑啊 2019-12-09
  • 打赏
  • 举报
回复
感谢lz,确实是jar包问题,不过我还把contentType写成contextType,属实无语
换个名字真难 2019-08-21
  • 打赏
  • 举报
回复
我也遇到这样的问题了,用两种方式都报错
appleYQL 2017-05-21
  • 打赏
  • 举报
回复
统一回复,找到原因了,谢谢大家,此贴终结:之前我导入的是:
现在导入的包换成:,这样就参数绑定时就不会报错。
ScottJane 2017-05-17
  • 打赏
  • 举报
回复
引用 8 楼 ScottJane 的回复:
[quote=引用 5 楼 appleYQL 的回复:] [quote=引用 4 楼 ScottJane 的回复:] [quote=引用 3 楼 ScottJane 的回复:]
@ResponseBody
    public String requestJson(@RequestBody Student student){
         JSONObject json = JSONObject.fromObject(student);//将java对象转换为json对象  
        String str = json.toString();//将json对象转换为字符串  
        return str;  
    }
要手动将Student转换为json字符串,前端才能接受到。[/quote] 不是返回的时候报错,而是它在绑定参数的时候就报错的[/quote] 前端数据可以直接封装成对象{"student":{"name":"scott","age":12}} 这样子 public String requestJson(@ModelAttribute(value = "stu")Student student)这样子获取对象[/quote] 只要前端对象的名称stu要和@ModelAttribute(value = "stu")中value一致。
ScottJane 2017-05-17
  • 打赏
  • 举报
回复
引用 5 楼 appleYQL 的回复:
[quote=引用 4 楼 ScottJane 的回复:] [quote=引用 3 楼 ScottJane 的回复:]
@ResponseBody
    public String requestJson(@RequestBody Student student){
         JSONObject json = JSONObject.fromObject(student);//将java对象转换为json对象  
        String str = json.toString();//将json对象转换为字符串  
        return str;  
    }
要手动将Student转换为json字符串,前端才能接受到。[/quote] 不是返回的时候报错,而是它在绑定参数的时候就报错的[/quote] 前端数据可以直接封装成对象{"student":{"name":"scott","age":12}} 这样子 public String requestJson(@ModelAttribute(value = "stu")Student student)这样子获取对象
李德胜1995 2017-05-17
  • 打赏
  • 举报
回复
jackson.jar的版本多少???
appleYQL 2017-05-17
  • 打赏
  • 举报
回复
引用 1 楼 u011619071 的回复:
声明你的接口 支持application/json
是改成这样的吗:
@RequestMapping(value="/requestJson.action",method=RequestMethod.POST
			,consumes="application/json;charset=utf-8",produces="application/json;charset=utf-8")
	@ResponseBody
	public Student requestJson(@RequestBody Student student){
		
		
		return student;
		
	}
这样还是会报这个错误
appleYQL 2017-05-17
  • 打赏
  • 举报
回复
引用 4 楼 ScottJane 的回复:
[quote=引用 3 楼 ScottJane 的回复:]
@ResponseBody
    public String requestJson(@RequestBody Student student){
         JSONObject json = JSONObject.fromObject(student);//将java对象转换为json对象  
        String str = json.toString();//将json对象转换为字符串  
        return str;  
    }
要手动将Student转换为json字符串,前端才能接受到。[/quote] 不是返回的时候报错,而是它在绑定参数的时候就报错的
ScottJane 2017-05-17
  • 打赏
  • 举报
回复
引用 3 楼 ScottJane 的回复:
@ResponseBody
    public String requestJson(@RequestBody Student student){
         JSONObject json = JSONObject.fromObject(student);//将java对象转换为json对象  
        String str = json.toString();//将json对象转换为字符串  
        return str;  
    }
要手动将Student转换为json字符串,前端才能接受到。
ScottJane 2017-05-17
  • 打赏
  • 举报
回复
@ResponseBody
    public String requestJson(@RequestBody Student student){
         JSONObject json = JSONObject.fromObject(student);//将java对象转换为json对象  
        String str = json.toString();//将json对象转换为字符串  
        return str;  
    }
天涯共明月 2017-05-17
  • 打赏
  • 举报
回复
__努力的小白 2017-05-17
  • 打赏
  • 举报
回复
我记得之前看过一篇帖子,说springmvc ajax不能加contentType:'application/json;charset=UTF-8', 这一行
X元素 2017-05-16
  • 打赏
  • 举报
回复
声明你的接口 支持application/json

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧