【问题标题】:Spring setDisallowedFields is not working弹簧 setDisallowedFields 不起作用
【发布时间】:2013-11-29 21:05:10
【问题描述】:

注册控制器不允许通过以下方式发送帐户 id 字段:

@InitBinder
public void initBinder(WebDataBinder binder) {
    binder.setDisallowedFields("id");
    binder.setRequiredFields("username","password","emailAddress");
} 

@RequestMapping(method = { RequestMethod.POST, RequestMethod.PUT })
public String handleRegistration(@ModelAttribute Account account, BindingResult result) {
    if (result.hasErrors()) {
        return "customer/register";
    }

我运行以下测试以确保不允许使用 ID:

@Test
public void testPutRequestWithIdPassedRegistrationController() throws Exception {
    this.mockMvc.perform(post("/customer/register")
            .param("id", "1")
            .param("username", "shouldBeIgnored")
            .param("password", "123")
            .param("emailAddress", "testIgnored@gmail.com")
            .param("address.country", "RU")
            .param("address.city", "Nsk")
            .param("address.street", "Lenin"))
            .andExpect(model().hasErrors())
            .andExpect(view().name("customer/register"));
}

但测试失败原因: java.lang.AssertionError:预期的绑定/验证错误

这里的比较是在不通过不可空字段的情况下尝试创建帐户的测试并且它通过了很好,这意味着 setRequiredFields 工作正常:

@Test
public void testPutRequestWithoutNeededFieldsRegistrationController() throws Exception {
    this.mockMvc.perform(post("/customer/register"))
            .andDo(print())
            .andExpect(status().isOk())
            .andExpect(view().name("customer/register"))
            .andExpect(model().hasErrors())
            .andExpect(model().errorCount(3));
}

为什么它会以这种方式工作?如何确定不允许使用该 id?

【问题讨论】:

    标签: java validation spring-mvc testing spring-mvc-test


    【解决方案1】:

    Spring 不会将不允许的字段视为错误。 它只是将它们作为suppressedFields 存储在BindException 中。 在调试期间,我可以通过以下方式访问它:

    ((BindingResult)getModelAndView(result).getModelMap().get("org.springframework.validation.BindingResult.account")).getSuppressedFields()
    

    hasErrors() 方法调用时。

    所以为了确保不使用 id,我只是通过 params 传递它,然后检查具有该名称的帐户(它是一个唯一字段)是否具有另一个 id 值:

    String notExistingId = "999";
    String newUserName = "newUser";
    this.mockMvc.perform(post("/customer/register")
            .param("id", notExistingId)
            .param("username", newUserName)
            .param("password", "123")
            .param("emailAddress", "testIgnored@gmail.com")
            .param("address.country", "RU")
            .param("address.city", "Nsk")
            .param("address.street", "Lenin"))
            .andExpect(model().hasNoErrors())
            .andExpect(view().name("redirect:/index.htm"));
    Optional<Account> account = accountService.getAccount(newUserName);
    assertTrue( "Account with the username should exist", account.isPresent());
    assertNotSame("Account id should not be equal to the id we try to pass with parameters",
            Long.parseLong(notExistingId),
            account.get().getId());
    

    【讨论】:

    • 我得到了 getModelAndView() 的未定义方法。它是用户定义的方法吗?如果是这样,它会做什么?
    猜你喜欢
    • 1970-01-01
    • 2011-01-02
    • 2011-06-22
    • 2013-05-05
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多