1

Suppose my form fields map to an Integer, Short, etc., and the input is invalid (non-numeric).

I get the following in my errors map upon form submission:

Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Short' for property 'property'; nested exception is java.lang.NumberFormatException: For input string: "a" 

I need to show a custom, user-friendly message. This doesn't work for me, it never comes here:

@ExceptionHandler(NumberFormatException.class)
public String handleNumberConversion(NumberFormatException nfe)
{
    return "error.invalidNumberFormat";
}  

BindException doesn't work either - in fact even a generic Exception in the method signature won't get activated.

EDIT: 2nd Attempt: I even tried this, this doesn't work either in my Resource Bundle, either:

# SpringMVC Error Overrides
typeMismatch.java.lang.NumberFormatException=A NumberFormatException occurred.
typeMismatch.java.lang.Short={0} is an invalid value.
typeMismatch.java.lang.Integer={0} is an invalid value.

Also, I need to get some details from the Binding process exception, such as the field that was being bound and the current value, because my custom error message shows this information.

8
  • well the exception handler can be assigned globally or per controller , which of the two are you using . also if you could post your spring config to check that the annotations scanner is being set up. Commented Nov 17, 2015 at 18:36
  • Yes I am using <context:annotation-config />, and also have <context:component-scan base-package="myapp" />. All other annotations work and get picked up. Commented Nov 17, 2015 at 18:39
  • and you have placed the ExceptionHandler method in the controller that throws the exception? Commented Nov 17, 2015 at 18:40
  • Yes. Also verified that method never gets called. Commented Nov 17, 2015 at 18:42
  • ok try to add this method as well and check if it gets called @ExceptionHandler(Exception.class) public void handleError(HttpServletRequest req, Exception exception) { logger.error("Request: " + req.getRequestURL() + " raised " + exception); } Commented Nov 17, 2015 at 18:44

1 Answer 1

1

Your handler method needs a different signature:

@ExceptionHandler(BindException.class)
public String handleBindException(BindException be) {
    // extract data from exception...
    return "whatever";
}
Sign up to request clarification or add additional context in comments.

3 Comments

Didn't work. In fact even an @ExceptionHandler(Exception.class) generically is never reached. The standard message is displayed instead.
@gene b. do you have some type of SimpleMappingExceptionResolver specified in app?
No, I don't. But I think I know what the problem is: My page handler takes a BindingResult parameter. According to this thread, a BindingResult param will prevent any BindExceptions from getting thrown: stackoverflow.com/a/22025320/1005607

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.