Search code examples
springspring-validator

Spring Validator throws NotReadablePropertyException on POST


I am trying to validate an object and return a meaningful response message using the Spring Validator.

I want it to check for a condition that a given String can only be of 5 certain values.

What's instead happening is that I am getting an NotReadablePropertyException thrown and returning basically nothing..

What am I doing wrong?

@Override
public void validate(Object obj, Errors error) {
    Jobs job = (Jobs) obj;
    String recurrence = job.getRecurrence();

    if(!recurrence.equals(RecurrenceStatus.TEST) 
            && !recurrence.equals(RecurrenceStatus.DAILY) && !recurrence.equals(RecurrenceStatus.FREQUENTLY) 
            && !recurrence.equals(RecurrenceStatus.WEEKLY) && !recurrence.equals(RecurrenceStatus.MONTHLY)){

        error.rejectValue("RECURRENCE STATUS", "422", "The recurrence status must be one of these: " + RecurrenceStatus.ALLSTATUS);
    }
}

Solution

  • When you look at the signature of rejectValue(java.lang.String field, java.lang.String errorCode, java.lang.String defaultMessage) you can see that the first parameter is field. It refers to a field in your Jobs class.

    I don’t think that your current value for field = "RECURRENCE STATUS" does that!

    You should change it to the referenceing field of your Jobs class.

    Check out the Spring Errors documentation.