0
使用傑克遜閱讀JSON對象,我可以能夠將對象轉換爲JSONSpring的MVC傑克遜不是從JQuery的
@RequestMapping(value="getMessage.test", headers = "Accept=application/json" ,method = RequestMethod.POST)
public @ResponseBody TestObject getMessage(){
TestObject object=new TestObject();
object.setMessage("Hello JQuery");
return object;
}
上面的代碼工作得很好。但是轉換從JSON到對象給出WARN PageNotFound - 找不到匹配的servlet請求處理程序方法:path'/setMessage.test',方法'POST',參數map [[empty]]以下是代碼。我要去的地方錯了..
@RequestMapping(value="setMessage.test", method = RequestMethod.POST, headers = "Accept=application/json" )
public void setMessage(@RequestBody TestObject test,HttpServletRequest request){
System.out.println("Inside setting message");
System.out.println(test.getMessage());
}
JQuery的Ajax調用...
$.ajaxSetup({ contentType: "application/json; charset=UTF-8" });
$.post('getMessage.test',function(response) {
alert(response.message);
}, 'json');
$.post('setMessage.test', {message: 'Hello Spring'});
工作,如果我有回調函數$。員額發佈('setMessage.test',{message:'Hello Spring'},function(response){alert(response.message);},'json');但是我得到org.springframework.http.converter.HttpMessageNotReadableException:無法讀取JSON:意外字符('m'(code 109)):預期有效值(數字,字符串,數組,對象,'true','false'或'null')**發送的內容不是有效的JSON?** – james007 2012-02-11 09:12:57
您是否試過'{「message」:「Hello Spring」}'insted of'{message:「Hello Spring」}? – 2012-02-13 07:35:28