0

我得到异常

org.springframework.beans.NotReadablePropertyException: Invalid property 'entries[0].reason' of bean class [my.company.data.SDROrder]: Bean property 'entries[0].reason' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

从以下代码片段:

Errors errors = new BeanPropertyBindingResult(new SDROrder(), "sdr");
orderValidator.validate(order, errors);

对于验证器:

public class OrderValidator implements Validator
{

    @Override
    public boolean supports(Class<?> clazz)
    {
        return Order.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(final Object target, final Errors errors)
    {
        errors.rejectValue("entries[0].reason", "Wrong Reason");
    }

}

我们有这样的数据层次结构

public class Order
{
    private List<AbstractOrderEntry> entries;

    public List<AbstractOrderEntry> getEntries()
    {
        return entries;
    }

    public void setEntries(List<AbstractOrderEntry> entries)
    {
        this.entries = entries;
    }
}

public class SDROrder extends Order
{

}

public class AbstractOrderEntry
{

}


public class SDROrderEntry extends AbstractOrderEntry
{
    private String reason;

    public String getReason()
    {
        return reason;
    }

    public void setReason(String reason)
    {
        this.reason = reason;
    }
}

请在此处查看工作示例:此处

更新1:只是为了澄清。问题是我尝试在具有对象集合的对象上拒绝值,其中每个元素在运行时具有特定属性但在编译时没有。Spring 使用 Bean 的属性来解析这些字段并且找不到继承的属性。问题是:我可以解释 Spring 以某种方式解决继承的字段吗?

4

1 回答 1

0

我在这里找到了解决方案。诀窍在于

  org.springframework.validation.Errors.pushNestedPath(String)

org.springframework.validation.Errors.popNestedPath()

方法。

正确的验证应该如下进行:

    errors.pushNestedPath("entries[0]");
    errors.rejectValue("reason", "Wrong Reason");
    errors.popNestedPath();
于 2014-05-19T09:43:09.187 回答