客户端代码:
$.ajax({
type: "POST",
url: "../web/zittles",
data: jsonformatdata,
contentType: "application/json",
dataType: "json",
success: function(data)
{
alert("data from server : "+data);
},
error: function(jqXHR, textStatus, errorThrown)
{
alert("jqXHR.status = "+jqXHR.status); //getting status code 400 here
}
});
输出json数据:
{
"id": 1,
"No": "1234",
"Desc": "Testing"
}
Java类:
public class Fizzle implements Serializable
{
private String id;
private String No;
private String Desc;
// getters and setters
}
弹簧 3 控制器:
@RequestMapping(value = '/zittles', method = RequestMethod.POST, headers ="Content-Type=application/json")
public @ResponseBody void doSomeThing (@RequestBody Fizzle fizzle) {
//do something here
}
app-servlet.xml 有
<mvc:annotation-driven/>
Tomcat的/lib文件夹有
jackson-core-lgpl-1.9.10.jar
jackson-mapper-lgpl-1.9.10.jar
收到状态码 400 错误 -
"The request sent by the client was syntactically incorrect"
当我如下所示更改控制器代码时,它将 json 数据作为字符串。
public @ResponseBody void doSomeThing (@RequestBody String fizzle) {}
理想情况下,杰克逊应该自动将 json 数据映射到 Fizzle 对象。
我在这里想念的是什么。要正确配置 Jackson 解析器,还需要做些什么吗?
请帮忙。