当前位置: 代码迷 >> Web前端 >> org.springframework.web.HttpMediaTypeNotAcceptableException
  详细解决方案

org.springframework.web.HttpMediaTypeNotAcceptableException

热度:1826   发布时间:2012-10-06 17:34:01.0
org.springframework.web.HttpMediaTypeNotAcceptableException:

From:http://cmzx3444.iteye.com/blog/930732

js代码:?

Java代码? ?收藏代码
  1. function?ajaxLogin()?{??
  2. ????$.ajax({type:"post" ,?url: "ajaxLogin.do" ,?data: "username=acid&password=acid" ,?success:function?(account)?{??
  3. ????????alert("Data?Saved:?" ?+?account.username);??
  4. ????}});??
  5. }??



Controller中的方法:?

Java代码? ?收藏代码
  1. @RequestMapping (value?=? "/ajaxLogin.do" ,?method?=?RequestMethod.POST)??
  2. public ? @ResponseBody ??
  3. Account?ajaxLogin(HttpServletRequest?request,?@Valid ??
  4. Account?account,?BindingResult?result)??
  5. {??
  6. ????account?=?accountService.getAccount(account.getUsername(),?account.getPassword());??
  7. ????if ?(account?==? null )??
  8. ????{??
  9. ????????return ? null ;??
  10. ????}??
  11. ????Utils.setToSession(SessionKey.USER_ID,?account.getUsername());??
  12. ????return ?account;??
  13. }??



结果出现了这个异常:?

Java代码? ?收藏代码
  1. org.springframework.web.HttpMediaTypeNotAcceptableException:?Could?not?find?acceptable?representation??
  2. ????at?org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.writeWithMessageConverters(AnnotationMethodHandlerAdapter.java:993 )??
  3. ????at?org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.handleResponseBody(AnnotationMethodHandlerAdapter.java:933 )??
  4. ????at?org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter$ServletHandlerMethodInvoker.getModelAndView(AnnotationMethodHandlerAdapter.java:882 )??
  5. ????at?org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:428 )??
  6. ????at?org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:414 )??
  7. ????at?org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790 )??
  8. ????at?org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719 )??
  9. ????at?org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644 )??
  10. ????at?org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549 )??
  11. ????at?javax.servlet.http.HttpServlet.service(HttpServlet.java:617 )??
  12. ????at?javax.servlet.http.HttpServlet.service(HttpServlet.java:717 )??
  13. ????at?org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290 )??
  14. ????at?org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206 )??
  15. ????at?com.shop.core.context.ContextInitFilter.doFilter(ContextInitFilter.java:33 )??
  16. ????at?org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235 )??
  17. ????at?org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206 )??
  18. ????at?org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233 )??
  19. ????at?org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191 )??
  20. ????at?org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127 )??
  21. ????at?org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102 )??
  22. ????at?org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109 )??
  23. ????at?org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298 )??
  24. ????at?org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857 )??
  25. ????at?org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588 )??
  26. ????at?org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489 )??
  27. ????at?java.lang.Thread.run(Thread.java:662 )??


看了看官方的demo,好像用到了jackson这个包。加上这个包后一切正常!?
原因估计是序列化这个account对象的时候出了问题。因为我直接返回string对象不会出错。

  相关解决方案