【发布时间】:2012-12-13 18:22:04
【问题描述】:
我正在尝试使用 Spring JSR303 验证来验证对象,我有一个表单对象,其中包含一些嵌套对象以及一些表单属性,这是我的表单签名
public class PaymentDetailsForm
{
private AddressForm billingAddress;
// other properties and getter and setters
}
在我的AddressForm bean 中,我使用了 Bean 验证注解来验证数据,但我没有在我的 PaymentDetailsForm 中为 billingAddress 使用任何 @Valid 注解。
这是我的 Controller 方法的签名
public String createUpdatePaymentInfos(final Model model,
@ModelAttribute("paymentInfo") @Valid final PaymentDetailsForm form, final BindingResult bindingResult)
{
}
如果我从表单发送正确的数据,一切正常,但如果我省略了 billingAddress 中标记为必需或不为空的任何字段,我将收到以下绑定错误异常
org.springframework.validation.BeanPropertyBindingResult: 1 errors
Field error in object 'paymentInfo' on field 'billingAddress':
rejected value [com.xxx.storefront.forms.AddressForm@e39f6f1,true];
codes [typeMismatch.paymentInfo.billingAddress,typeMismatch.billingAddress,typeMismatch.com.xxx.storefront.forms.AddressForm,typeMismatch];
arguments [org.springframework.context.support.DefaultMessageSourceResolvable:
codes [paymentInfo.billingAddress,billingAddress]; arguments []; default message [billingAddress]];
default message [Failed to convert property value of type 'java.lang.String[]'
to required type 'com.xxx.storefront.forms.AddressForm' for property 'billingAddress';
nested exception is java.lang.IllegalStateException:
Cannot convert value of type [java.lang.String[]] to required type [com.xxx.storefront.forms.AddressForm] for property 'billingAddress':
no matching editors or conversion strategy found]
我期待由于我没有为 billingAddress 属性使用 @valid 注释,因此不应该对其进行验证,但即使它得到验证,我也无法理解上述异常/错误
【问题讨论】:
标签: spring spring-mvc bean-validation