Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring MVC Hibernate Validator TypeMismatch Exception
    primarykey
    data
    text
    <p>I'm using Spring MVC with Hibernate validator 4.2.0. have a ValidationMessages.properties on my class path in /WEB-INF/classes/ValidationMessages.properties:</p> <pre><code>typeMismatch.java.lang.Integer=Must specify an integer value. typeMismatch.int=Invalid number entered typeMismatch=Invalid type entered </code></pre> <p>This is made available as a bean in javaconfig:</p> <pre><code>@Configuration @EnableWebMvc public class WebApplicationConfiguration extends WebMvcConfigurerAdapter { ... @Bean public ReloadableResourceBundleMessageSource messageSource() { ReloadableResourceBundleMessageSource resourceBundleMessageSource = new ReloadableResourceBundleMessageSource(); resourceBundleMessageSource.setBasename("ValidationMessages"); return resourceBundleMessageSource; } ... </code></pre> <p>which is loading ValidationMessages.properties from the class path fine. My Controller:</p> <pre><code>@Controller public class myController { ... @InitBinder("myForm") protected void initUserBinder(WebDataBinder binder) { binder.setValidator(new CustomValidator()); } ... @ResponseBody @RequestMapping(value = "/ajax/myRequest", method = RequestMethod.POST) public CustomResponse ProcessAjaxRequest( @Valid @ModelAttribute final MyForm myForm, final BindingResult bindingResult) throws Exception { if (bindingResult.hasErrors()) { return new CustomResponse(bindingResult.getAllErrors()); } else { .. } } ... </code></pre> <p>And a custom validator:</p> <pre><code>public class CustomValidator implements Validator { @Override public boolean supports(Class c) { return MyForm.class.equals(c); } @Override public void validate(Object obj, Errors errors) { .. </code></pre> <p>Validation with my CustomValidator works fine (I insert the error messages manually, not using the message source), however for binding typeMismatch errors I get the exception:</p> <pre><code>Failed to convert property value of type 'java.lang.String' to required type 'java.lang.Integer' for property 'myField'; nested exception is java.lang.NumberFormatException: For input string: "A" </code></pre> <p>rather than the code from ValidationMessages.properties, so it looks as though the DataBinder (?) is not using my messageSource. I want the typeMismatch codes from my properties file rather than the exception message. I have also tried with ResourceBundleMessageSource instead of ReloadableResourceBundleMessageSource but that didn't make any difference. Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload