Question

I get Exception

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?

from the following code snippet:

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

for validator:

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");
    }

}

where we have such data hierarchy

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;
    }
}

Please see working example here: here

Update 1: Just to clarify. The problem is I try to rejectValue on object that has Collection of objects where each element has specific attribute at Runtime but has not it at Compile time. Spring uses Bean's properties to resolve these fields and can't find inherited attribute. The question is: can I explain Spring to resolve inherited fields somehow?

Was it helpful?

Solution

I found the solution here. The trick is at

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

and

org.springframework.validation.Errors.popNestedPath()

methods.

The correct validation should be done as follow:

    errors.pushNestedPath("entries[0]");
    errors.rejectValue("reason", "Wrong Reason");
    errors.popNestedPath();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top