spring参数类型异常输出,SpringMvc参数类型转换错误输出
spring参数类型异常输出,
SpringMvc参数类型转换错误输出
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2016年6月6日 17:01:47 星期一
http://fanshuyao.iteye.com/
有一种更加方便的方法,可以全局使用,详情见:http://fanshuyao.iteye.com/blog/2303366
一、问题描述:
一般情况下,Spring对于方法接收参数的错误是没有提示的,而是直接输入到一个错误的页面,但从错误页面是看不出到底是什么错,如下面的例子
@RequestMapping("/test") public String test(int i){ System.out.println(i); int a = 2/0; throw new RuntimeException("出错了!"); }
http://localhost:8080/project/test?i=a
参数i是整型,但我在请求时是一个字符串a,出错后显示的页面如下:
一般情况是知道的,但如果在参数是对象(如User user)的情况下,如果类型转换出错,就很难知道了。
二、解决方法:
在对应的方法加上如下注解:
@ExceptionHandler({TypeMismatchException.class })
如下面的例子所示:
@RequestMapping("/test") @ExceptionHandler({TypeMismatchException.class }) public String test(int i){ System.out.println(i); int a = 2/0; throw new RuntimeException("出错了!"); }
再请求时,就会在控制台输出错误信息
java.lang.IllegalStateException: No suitable resolver for argument [0] [type=int] HandlerMethod details: Controller [com.chinagas.biz.controller.ExceptionController] Method [public java.lang.String com.chinagas.biz.controller.ExceptionController.test(int)] at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:175) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:129) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110) at org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver.doResolveHandlerMethodException(ExceptionHandlerExceptionResolver.java:362) at org.springframework.web.servlet.handler.AbstractHandlerMethodExceptionResolver.doResolveException(AbstractHandlerMethodExceptionResolver.java:60) at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:138) at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1167) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1004) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857) at javax.servlet.http.HttpServlet.service(HttpServlet.java:624) at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842) at javax.servlet.http.HttpServlet.service(HttpServlet.java:731) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:957) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:620) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:318) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Unknown Source)
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
蕃薯耀 2016年6月6日 17:01:47 星期一
http://fanshuyao.iteye.com/
今天越懒,明天要做的事越多。