controller作为后台与前台的ajax进行交互,后台的方法处理完成返回一个boolean类型的值,想传给前台用来判断是否执行成功,BUT,问题来了:
1 严重: Servlet.service() for servlet rest threw exception 2 java.lang.IllegalArgumentException: Unknown return value type [java.lang.Boolean] 3 at org.springframework.util.Assert.notNull(Assert.java:112) 4 at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:70) 5 at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:126) 6 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777) 7 at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706) 8 at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) 9 at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943) 10 at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) 11 at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) 12 at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868) 13 at javax.servlet.http.HttpServlet.service(HttpServlet.java:643) 14 at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) 15 at javax.servlet.http.HttpServlet.service(HttpServlet.java:723) 16 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 17 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 18 at com.agen.util.LoginFilter.doFilter(LoginFilter.java:58) 19 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 20 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 21 at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:150) 22 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 23 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 24 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 25 at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) 26 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) 27 at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 28 at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 29 at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 30 at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 31 at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 32 at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) 33 at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 34 at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 35 at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:879) 36 at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:617) 37 at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1778) 38 at java.lang.Thread.run(Unknown Source)
此刻的controller的代码是这样的:
1 @RequestMapping("/productAdd") 2 3 public boolean productAdd(Product product){ 4 product.setProductId(UUID.randomUUID().toString()); 5 Criteria criteria = getCurrentSession().createCriteria(Product.class); 6 criteria.setProjection(Projections.max("productOrder")); 7 Integer productOrder2 = productService.uniqueResultInt(criteria); 8 Integer procuctOrder=1; 9 if(productOrder2 == null){ 10 product.setProductOrder(procuctOrder); 11 }else{ 12 productOrder2++; 13 product.setProductOrder(productOrder2); 14 } 15 productService.save(product); 16 return true; 17 }
问题:
此刻的controller只能识别返回的true作为字符串,也就是URL去找对应的页面,但是哪里有URL是这样的。
分析问题:
而且我们是想把这个true作为返回值给前台的ajax来判断的,如果为true则插入成功给个提示等等,所以我们不想让这个true作为URL去返回,而是作为JSON格式的数据返回给前台
解决方法:
我们需要给这个方法上加一个 spring的注解,让方法返回一个json数据给前台 这个注解就是:@ResponseBody
1 @RequestMapping("/productAdd") 2 @ResponseBody 3 public boolean productAdd(Product product){ 4 product.setProductId(UUID.randomUUID().toString()); 5 Criteria criteria = getCurrentSession().createCriteria(Product.class); 6 criteria.setProjection(Projections.max("productOrder")); 7 Integer productOrder2 = productService.uniqueResultInt(criteria); 8 Integer procuctOrder=1; 9 if(productOrder2 == null){ 10 product.setProductOrder(procuctOrder); 11 }else{ 12 productOrder2++; 13 product.setProductOrder(productOrder2); 14 } 15 productService.save(product); 16 return true; 17 }
如此 就可以将true作为JSON数据返回给前台了