From:http://cmzx3444.iteye.com/blog/930732
js代码:?
- function?ajaxLogin()?{??
- ????$.ajax({type:"post" ,?url: "ajaxLogin.do" ,?data: "username=acid&password=acid" ,?success:function?(account)?{??
- ????????alert("Data?Saved:?" ?+?account.username);??
- ????}});??
- }??
Controller中的方法:?
- @RequestMapping (value?=? "/ajaxLogin.do" ,?method?=?RequestMethod.POST)??
- public ? @ResponseBody ??
- Account?ajaxLogin(HttpServletRequest?request,?@Valid ??
- Account?account,?BindingResult?result)??
- {??
- ????account?=?accountService.getAccount(account.getUsername(),?account.getPassword());??
- ????if ?(account?==? null )??
- ????{??
- ????????return ? null ;??
- ????}??
- ????Utils.setToSession(SessionKey.USER_ID,?account.getUsername());??
- ????return ?account;??
- }??
结果出现了这个异常:?
- org.springframework.web.HttpMediaTypeNotAcceptableException:?Could?not?find?acceptable?representation??
- ????at?org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.writeWithMessageConverters(AnnotationMethodHandlerAdapter.java:993 )??
- ????at?org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.handleResponseBody(AnnotationMethodHandlerAdapter.java:933 )??
- ????at?org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.getModelAndView(AnnotationMethodHandlerAdapter.java:882 )??
- ????at?org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:428 )??
- ????at?org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414 )??
- ????at?org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790 )??
- ????at?org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719 )??
- ????at?org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644 )??
- ????at?org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549 )??
- ????at?javax.servlet.http.HttpServlet.service(HttpServlet.java:617 )??
- ????at?javax.servlet.http.HttpServlet.service(HttpServlet.java:717 )??
- ????at?org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290 )??
- ????at?org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206 )??
- ????at?com.shop.core.context.ContextInitFilter.doFilter(ContextInitFilter.java:33 )??
- ????at?org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235 )??
- ????at?org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206 )??
- ????at?org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233 )??
- ????at?org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191 )??
- ????at?org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127 )??
- ????at?org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102 )??
- ????at?org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109 )??
- ????at?org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298 )??
- ????at?org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857 )??
- ????at?org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588 )??
- ????at?org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489 )??
- ????at?java.lang.Thread.run(Thread.java:662 )??
看了看官方的demo,好像用到了jackson这个包。加上这个包后一切正常!?
原因估计是序列化这个account对象的时候出了问题。因为我直接返回string对象不会出错。