博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【spring 注解 错误】使用controller 作为后台给前台ajax交互数据出错
阅读量:6680 次
发布时间:2019-06-25

本文共 5520 字,大约阅读时间需要 18 分钟。

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)
View Code

 

此刻的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     }
View Code

 

问题:

此刻的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     }
View Code

 

如此 就可以将true作为JSON数据返回给前台了

 

转载地址:http://jerxo.baihongyu.com/

你可能感兴趣的文章
新博安卓培训的第一天
查看>>
游戏中常用到的碰撞检测帮助类
查看>>
访问默认共享
查看>>
01262015要看的blog——oracle tuning
查看>>
[信息图]电子商务营销的6大步骤
查看>>
Hibernate注释大全收藏
查看>>
通过openfiler模拟存储
查看>>
java学习笔记 --- String类
查看>>
1.5-cut命令
查看>>
我的友情链接
查看>>
从技术角度看人与人的沟通
查看>>
加速sshd
查看>>
15.3、SElinux介绍
查看>>
关于Nagios Core
查看>>
python基本数据类型的介绍
查看>>
原生的js写Ajax请求
查看>>
战略合作背后的秘密:VMware沦为AWS的渠道商?
查看>>
tar.gz安装
查看>>
Centos6.5 glibc 升级
查看>>
排序——C++函数调用
查看>>