Skip to content

MVC: which parameter cannot be parsed? [SPR-11805] #16425

Closed

Description

@spring-projects-issues

Stepan Koltsov opened SPR-11805 and commented

When Spring MVC cannot parse request parameter, exception message does not contain information about which parameter cannot be parsed. So it is hard to find what's wrong looking in logs.

Test case:

@EnableAutoConfiguration
@Controller
public class Bug1 {

    @RequestMapping("/")
    public String index(@RequestParam("aa") int aa, @RequestParam("bb") int bb) {
        return "hello world";
    }

    @ExceptionHandler
    public String foo(Throwable throwable) {
        throwable.printStackTrace();
        return "exception handled";
    }

    public static void main(String[] args) {
        SpringApplication.run(Bug1.class, args);
    }
}

Exception is:

org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "1.1"
	at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:77)
	at org.springframework.beans.TypeConverterSupport.convertIfNecessary(TypeConverterSupport.java:47)
	at org.springframework.validation.DataBinder.convertIfNecessary(DataBinder.java:582)
	at org.springframework.web.method.annotation.AbstractNamedValueMethodArgumentResolver.resolveArgument(AbstractNamedValueMethodArgumentResolver.java:105)
	at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:79)
	at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:157)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:124)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
	at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:108)
	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:501)
	at org.apache.catalina.valves.RemoteIpValve.invoke(RemoteIpValve.java:683)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
	at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1040)
	at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:607)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1721)
	at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1679)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NumberFormatException: For input string: "1.1"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.lang.Integer.parseInt(Integer.java:580)
	at java.lang.Integer.valueOf(Integer.java:766)
	at org.springframework.util.NumberUtils.parseNumber(NumberUtils.java:155)
	at org.springframework.beans.propertyeditors.CustomNumberEditor.setAsText(CustomNumberEditor.java:113)
	at org.springframework.beans.TypeConverterDelegate.doConvertTextValue(TypeConverterDelegate.java:430)
	at org.springframework.beans.TypeConverterDelegate.doConvertValue(TypeConverterDelegate.java:403)
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:181)
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:93)
	at org.springframework.beans.TypeConverterSupport.doConvert(TypeConverterSupport.java:64)
	... 41 more

open http://localhost:8080/?aa=1.1&bb=2

Parameter aa cannot be parsed, but I cannot tell it from exception stack trace.


Affects: 4.0.2

Issue Links:

2 votes, 4 watchers

Activity

  1. spring-projects-issues commented on May 19, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Okay we could raise a more helpful exception if conversion fails in AbstractNamedValueMethodArgumentResolver, probably MethodArgumentNotValidException. That would cover a few other method argument types too that depend on conversion.

    BTW in development you should be able to get more information about the failure with exact method and argument information at DEBUG log level. I'm guessing however this is in production.

  2. spring-projects-issues commented on Jul 22, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    On a second thought changing the original exception might not be such a good idea for existing applications that already rely on it.

    For very detailed information on every failed argument resolution, enable TRACE for org.springframework.web.method.support.InvocableHandlerMethod and that will log the details including a stack trace regardless of how the exception is handled afterwards. Aside from that we can't log this information in production since we don't know how the exception will be handled.

  3. spring-projects-issues commented on Feb 13, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    Juergen Hoeller, assigning this one to you, for one more review, since you're also planning to look at #19384. Perhaps we can decorate the type conversion error properly, and it would be enough to ensure it maps to the same response code? Since type conversion is done in the base class, this would apply to more than just request parameters.

  4. spring-projects-issues commented on Jul 16, 2018

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    This seems to be a duplicate of #14786: As of 4.2, we're throwing a reasonably rich MethodArgumentConversionNotSupportedException already, wrapping the underlying ConversionNotSupportedException / TypeMismatchException.

  5. added
    type: enhancement
    status: duplicate
    in: web
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webstatus: duplicatetype: enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions