Spring + JSR 303验证组被忽略

时间:2012-12-06 10:46:53

标签: spring spring-mvc bean-validation

我们有一个带有JSR注释的简单bean

public class CustomerDTO {

    private static final long serialVersionUID = 1L;

    private Integer id;

    @NotEmpty(message = "{customer.firstname.empty}")
    private String firstName;

    @NotEmpty(message = "{customer.lastname.empty}")
    private String lastName;

    @NotEmpty(groups={PasswordChange.class}, message="{password.empty}")
    private String password;

    @NotEmpty(groups={PasswordChange.class}, message="{confirmation.password.empty}")
    private String password2;

}

我们有一个Spring Controller

@RequestMapping(value="/changePassword", method = RequestMethod.POST)
public String changePassword(@Validated({ PasswordChange.class }) @ModelAttribute("customerdto") CustomerDTO customerDTO, BindingResult result, Locale locale) {
    logger.debug("Change Password was submitted with information: " + customerDTO.toString());
    try {
        passwordStrengthPolicy.checkPasswordStrength(locale, customerDTO.getPassword());
        if (result.hasErrors()) {
            return "changePassword";
        }
        logger.debug("Calling customer service changePassword: " + customerDTO);
        customerOnlineNewService.changePassword(customerDTO);
    } catch (PasswordNotChangedException e) {
        logger.error("Could not change password PasswordNotChangedException: " + customerDTO.toString());
            return "changePassword";
    } catch (PasswordNotSecureException e) {
        return "changePassword";
    }
    return createRedirectViewPath("changePassword");
}

我们的问题是,当调用changePassword时,验证器会忽略该组(PasswordChange.class)并仅验证不在该组中的firstName和lastName。

有什么想法吗? 非常感谢你的时间。

1 个答案:

答案 0 :(得分:1)

可能是验证订单的一部分吗? (http://docs.oracle.com/cd/E19798-01/821-1841/gkahp/index.html)-->自定义组验证顺序 它听起来确实如此!