Spring MVC Hibernate验证程序类型不匹配异常

Spring MVC Hibernate验证程序类型不匹配异常,spring,validation,spring-mvc,hibernate-validator,spring-mvc-initbinders,Spring,Validation,Spring Mvc,Hibernate Validator,Spring Mvc Initbinders,我正在使用SpringMVC和HibernateValidator4.2.0。在/WEB-INF/classes/ValidationMessages.properties中的我的类路径上有一个ValidationMessages.properties: typeMismatch.java.lang.Integer=Must specify an integer value. typeMismatch.int=Invalid number entered typeMismatch=Invalid

我正在使用SpringMVC和HibernateValidator4.2.0。在/WEB-INF/classes/ValidationMessages.properties中的我的类路径上有一个ValidationMessages.properties:

typeMismatch.java.lang.Integer=Must specify an integer value.
typeMismatch.int=Invalid number entered
typeMismatch=Invalid type entered
这在javaconfig中作为bean提供:

@Configuration
@EnableWebMvc
public class WebApplicationConfiguration extends WebMvcConfigurerAdapter {
...

@Bean
public ReloadableResourceBundleMessageSource messageSource() {
    ReloadableResourceBundleMessageSource resourceBundleMessageSource = new ReloadableResourceBundleMessageSource();
    resourceBundleMessageSource.setBasename("ValidationMessages");
    return resourceBundleMessageSource;
}
...
正在从类路径加载ValidationMessages.properties。我的控制器:

@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 {
        ..
    }
}
...
和自定义验证程序:

public class CustomValidator implements Validator {

@Override
public boolean supports(Class c) {
    return MyForm.class.equals(c);
}

@Override
public void validate(Object obj, Errors errors) {
..
使用我的CustomValidator进行验证工作正常(我手动插入错误消息,而不是使用消息源),但是对于绑定类型不匹配错误,我会得到异常:

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"

而不是ValidationMessages.properties中的代码,因此看起来DataBinder(?)没有使用my messageSource。我想要属性文件中的类型不匹配代码,而不是异常消息。我也尝试过使用ResourceBundleMessageSource而不是ReloadableSourceBundleMessageSource,但没有任何区别。有什么想法吗?

您如何获得错误消息?这对你有用吗

@Autowired
private MessageSource messageSource;
...

FieldError error = bindingResult.getFieldError("fieldName");
String errorMessage = messageSource.getMessage(error, Locale.getDefault());

我使用的是构造函数注入,而不是自动连线,但我得到:未能将java.lang.String类型的属性值转换为属性fieldName所需的java.lang.Integer类型;嵌套的异常是java.lang.NumberFormatException:对于输入字符串:“A”看起来像是我的bean定义有问题吗?您的bean定义似乎很好。是否使用getDefaultMessage()获取错误消息?如果使用getDefaultMessage(),则无法获取自定义的错误消息。似乎Spring默认实现()在为BindingResult对象设置默认错误消息时未使用MessageSource。是的,这是原因-DefaultBindingErrorProcessor在为绑定错误设置错误消息时未使用MessageSource。因此,我使用MessageSource创建了自己的自定义DefaultBindingErrorProcessor,并在控制器中设置WebDataBinder.SetBindingerErrorProcessor。