Java Spring setDisallowedFields不工作
注册控制器不允许通过以下方式发送帐户id字段:Java Spring setDisallowedFields不工作,java,validation,spring-mvc,testing,spring-mvc-test,Java,Validation,Spring Mvc,Testing,Spring Mvc Test,注册控制器不允许通过以下方式发送帐户id字段: @InitBinder public void initBinder(WebDataBinder binder) { binder.setDisallowedFields("id"); binder.setRequiredFields("username","password","emailAddress"); } @RequestMapping(method = { RequestMethod.POST, RequestMet
@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:预期的绑定/验证错误
对于比较,这里有一个测试,它尝试在不传递不可为null的字段的情况下创建帐户,并且通过得很好,这意味着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是不允许的?< /P> < P> Spring并没有考虑不允许的字段作为错误。 它只是将它们存储为
BindException
中的suppressedFields
。
调试期间,我可以通过以下方式访问它:
((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());
String notExistingId=“999”;
字符串newUserName=“newUser”;
this.mockMvc.perform(post(“/customer/register”)
.param(“id”,notExistingId)
.param(“用户名”,newUserName)
.param(“密码”、“123”)
.param(“电子邮件地址”testIgnored@gmail.com")
.param(“地址、国家”、“RU”)
.param(“地址、城市”、“Nsk”)
.param(“地址街”、“列宁”))
.andExpect(model().hasNoErrors())
.andExpect(view().name(“重定向:/index.htm”);
可选帐户=accountService.getAccount(newUserName);
assertTrue(“应该存在用户名为的帐户”,Account.isPresent());
assertNotSame(“帐户id不应等于我们尝试通过参数传递的id”,
Long.parseLong(notExistingId),
account.get().getId());
我正在获取getModelAndView()的未定义方法。它是用户定义的方法吗?如果是这样的话,它将做什么?