gpt4 book ai didi

spring - Spring 启动中的@InitBinder无法与@RequestBody一起使用

转载 作者:行者123 更新时间:2023-12-03 12:28:05 24 4
gpt4 key购买 nike

如果我使用@InitBinder而不限制它,则它可以与@RequestBody一起正常使用以验证我的对象。

@InitBinder
private void initBinder(WebDataBinder binder) {
binder.setValidator(validator);
}



@RequestMapping(method=RequestMethod.POST)
public CustomerQuickRegisterEntity saveNewCustomer(@Valid @RequestBody CustomerQuickRegisterEntity customerEntity,BindingResult result)
{
if(result.hasErrors())
{
return new CustomerQuickRegisterEntity();
}
return customerQuickRegisterRepository.save(customerEntity);

}

但是问题是,当我通过像 @InitBinder("customerEntity")一样将其限制为一个对象时,它并没有验证该对象。因此,我在stackoverflow中进行了搜索,发现 @InitBinding仅适用于带有 @ModelAttribute注释的对象。然后我的问题是,当我将它用作 @RequestBody时,它可以与 @InitBinder一起正常工作,但是当我将其用作 @InitBinder("customerEntity")时却不能很好地工作...为什么这样?
还有其他方法可以验证与 @RequestBody关联的对象(不是单独的对象属性)

最佳答案

从文档中

Default is to apply to all command/form attributes and all request parameters processed by the annotated handler class. Specifying model attribute names or request parameter names here restricts the init-binder method to those specific attributes/parameters, with different init-binder methods typically applying to different groups of attributes or parameters.



请看看 here

关于spring - Spring 启动中的@InitBinder无法与@RequestBody一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27120058/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com