使用@Valid注释本身未检测到JSR-303错误

时间:2015-01-15 18:26:12

标签: spring spring-mvc bean-validation

为什么@Valid注释本身没有捕获我的JSR-303注释,但是使用以下方法捕获它们:

WebConfig.java

@Bean
public ResourceBundleMessageSource messageSource() {

    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();

    String[] strBaseNames = {
            "resources.messages.layout.LayoutResources",
            "resources.messages.layout.MenuResources",
            "resources.messages.global.GlobalResources"
    };

    messageSource.setUseCodeAsDefaultMessage(true);
    messageSource.setDefaultEncoding("UTF-8");
    messageSource.setBasenames(strBaseNames);

    return messageSource;
}

@Bean
public LocalValidatorFactoryBean jsr303Validator() {

    LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
    localValidatorFactoryBean.setValidationMessageSource(this.messageSource());

    return localValidatorFactoryBean;
}

UserController.java

@Controller
@RequestMapping(value = "/security/user", method = RequestMethod.GET)
public class UserController {

@Autowired
private SecurityService securityService;

@Autowired
SessionObject sessionObject;

@Autowired 
@Qualifier("jsr303Validator") Validator validator;

@RequestMapping(value = "/edit/validate", method = RequestMethod.POST)
public String validateEdit( @ModelAttribute @Valid User user,
                            BindingResult result,
                            Model model) {

    String strViewName = "common/error";

    validator.validate(user, result);

    if(result.hasErrors()) {
        strViewName = "user/userEdit";
    } else {
        ... update work ...
        strViewName = "user/success";
    }

    return strViewName;
}

资源: http://www.wenda.io/questions/2462924/convert-jsr-303-validation-errors-to-springs-bindingresult.html

如果我只使用@Valid注释,那么result.hasErrors()总是为空,所以我最终得到了一个ConstraintViolationException(见下文),这是预期的。但是我想让它只使用@Valid注释,而不必在我希望实现某些JSR-303验证的每个控制器中自动装配我的jsr303validator bean。

约束违规日志跟踪

Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [spring4base.model.security.User] during update time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='error.firstname.notnull', propertyPath=strFirstName, rootBeanClass=class spring4base.model.security.User, messageTemplate='error.firstname.notnull'}
ConstraintViolationImpl{interpolatedMessage='error.userid.notnull', propertyPath=strUserId, rootBeanClass=class spring4base.model.security.User, messageTemplate='error.userid.notnull'}
ConstraintViolationImpl{interpolatedMessage='error.lastname.notnull', propertyPath=strLastName, rootBeanClass=class spring4base.model.security.User, messageTemplate='error.lastname.notnull'}

User.java

@NotBlank(message = "error.userid.notnull")
@Size(min = 0, max = 14, message = "error.firstname.length")
private String strUserId = "";

@NotBlank(message = "error.firstname.notnull")
@Size(min = 0, max = 50, message = "error.firstname.length")
private String strFirstName = "";

@NotBlank(message = "error.lastname.notnull")
@Size(min = 0, max = 50, message = "error.lastname.length")
private String strLastName = "";

谢谢

1 个答案:

答案 0 :(得分:0)

为了使其适用于我的情况,需要以下内容:

首先,在我的Java Config类中定义一个LocalValidatorFactoryBean(在我的例子中是WebAppContext.java)。

@Bean
public ResourceBundleMessageSource messageSource() {

    ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();

    String[] baseNames = {
            "resources.messages.layout.LayoutResources",
            "resources.messages.layout.MenuResources",
            "resources.messages.option.AppConfigResources",
            "resources.messages.global.GlobalResources",
            "resources.messages.contact.ContactResources",
            "resources.messages.currentsession.CurrentSessionResources",
            "resources.messages.welcome.WelcomeResources",
            "resources.messages.user.UserResources",
            "resources.messages.role.RoleResources",
            "resources.messages.profile.ProfileResources"
    };

    messageSource.setCacheSeconds(60);
    messageSource.setFallbackToSystemLocale(false);
    messageSource.setUseCodeAsDefaultMessage(true);
    messageSource.setDefaultEncoding(StandardCharsets.UTF_8.name());
    messageSource.setBasenames(baseNames);

    return messageSource;
}


@Bean
public LocalValidatorFactoryBean jsr303Validator() {

    LocalValidatorFactoryBean localValidatorFactoryBean = new LocalValidatorFactoryBean();
    localValidatorFactoryBean.setValidationMessageSource(this.messageSource());

    return localValidatorFactoryBean;
}

然后,在我的控制器中,我像这样自动发送验证器:

@Autowired
private Validator jsr303Validator;

然后,我发现让我的JSR-303被执行的唯一方法是手动启动它们,如下所示:

@RequestMapping(value = "/update",  method = RequestMethod.POST)
public String processUpdate(@ModelAttribute UserForm userForm,
                            BindingResult result,
                            RedirectAttributes redirectAttributes,
                            SessionStatus status) throws CheckedPersistenceException {

    this.userFormValidator.validate(userForm, result);

    if (!result.hasErrors()) {
        this.jsr303Validator.validate(userForm, result);
    }

    if(result.hasErrors()) {
        return this.UPDATE_VIEW;
    }

    ...
}