Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
@spring引导中的InitBinder不能与@RequestBody一起工作_Spring_Spring Mvc_Spring Boot - Fatal编程技术网

@spring引导中的InitBinder不能与@RequestBody一起工作

@spring引导中的InitBinder不能与@RequestBody一起工作,spring,spring-mvc,spring-boot,Spring,Spring Mvc,Spring Boot,如果我使用@InitBinder而不加以限制,那么@RequestBody可以很好地验证我的对象 @InitBinder private void initBinder(WebDataBinder binder) { binder.setValidator(validator); } @RequestMapping(method=RequestMethod.POST) public CustomerQuickRegisterEntity saveNewCustomer(@Valid

如果我使用@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
仅适用于使用
@modeldattribute
注释的对象。然后我的问题是,当我将它用作
@InitBinder
时,它与
@RequestBody
一起工作很好,但当我将它用作
@InitBinder(“customerEntity”)
时,它工作不好……为什么会这样? 是否有其他方法来验证与文档中的
@RequestBody

关联的对象(而不是单独的对象属性)

默认设置是应用于所有命令/表单属性和所有请求 由带注释的处理程序类处理的参数。指定模型 此处的属性名称或请求参数名称限制 将init binder方法应用于这些特定属性/参数,并使用 不同的初始绑定方法通常适用于不同的组 属性或参数


请看一看

这是一个老问题,但我已设法获得
@InitBinder
注释,将我的自定义
验证器
绑定到
@Valid@RequestBody
参数,如下所示:

@InitBinder
private void bindMyCustomValidator(WebDataBinder binder) {
    if ("entityList".equals(binder.getObjectName())) {
        binder.addValidators(new MyCustomValidator());
    }
}

如果您试图通过设置注释的值来过滤绑定参数,那么它对于
@RequestBody
参数将不起作用。所以这里我检查对象名。我的方法参数实际上被称为
entities
,但Spring决定将其称为
entityList
。我必须调试它才能发现这一点。

您可以尝试我的解决方案:

@InitBinder
专用绑定器(WebDataBinder绑定器){
if(CustomerQuickRegisterEntity.class.equals(binder.getTarget().getClass())){
binder.addValidators(新的YourValidator());
}
}

它根本无法使用
@RequestBody
,因为绑定框架(
@modeldattribute
)与用于
@RequestBody
的消息转换框架完全不同。使用
@RequestBody
时不使用活页夹。始终调用带参数的方法,带参数的方法仅为名为
customerEntity
的模型对象调用,它不会被调用。但是当我将
@RequestBody
用作
@InitBinding
时,它可以很好地工作,而不会将其限制到任何特定对象。这是confusing@DineshShende看看您遇到的一个极端情况,因为
WebDataBinder
实际上用于验证。但是,当您使用名称对其进行限制时,它是modelAttribute的名称,而不是其他任意对象的名称。
@RequestBody
带注释的参数不是模型的一部分。@ankur singhal谢谢你,伙计……带第一个小写字母的类解决方案不起作用了。谢谢你的建议