Search code examples
resthibernate-validatorspring-hateoas

Validating a Spring ResourceSupport-ed parent resource as a not empty property in a child resource


I'm looking for guidelines into validating a parent admin resource (AdminResource extending the Spring ResourceSupport class) as not being empty (@NotEmpty) in a child admin module resource (AdminModuleResource extending the Spring ResourceSupport class).

I understand the AdminResource class should also implement the Serializable interface ? Is that the way to go with Spring ResourceSupport-ed resources ?

Here are my resources:

public class AdminResource extends AbstractResource {

    private String firstname;
    private String lastname;
    @NotEmpty
    @Email
    private String email;
    private String password;
    private String passwordSalt;

}

public class AdminModuleResource extends AbstractResource {

    @NotEmpty
    private String module;
    @NotEmpty
    private AdminResource adminResource;
}

public abstract class AbstractResource extends ResourceSupport {

    @JsonProperty("id")
    private Long resourceId;

    public AbstractResource() {
    }

    public Long getResourceId() {
        return resourceId;
    }

    public void setResourceId(Long resourceId) {
        this.resourceId = resourceId;
    }

}

As of now, the @NotEmpty validator annotation gives me the error: No validator could be found for type...

But adding the "implements Serializable" to the resources did not help and the exception remained when using the @NotEmpty validator annotation.

public abstract class AbstractResource extends ResourceSupport implements Serializable {
}

Of course, commenting out the @NotEmpty validator annotation makes the Maven build successful.

Thanks for any directions tips !

Kind Regards,

Stephane


Solution

  • @NotEmpty is only supported for CharSequences (String), Collections, Maps and arrays. It either checks whether the string or collection/array is empty. What does it even mean that a AdminResource is not empty. Do you mean @NotNull?

    If it really would make semantically sense to have a @NotEmpty for AdminResource, you would have to implement a custom ConstraintValidator for it and registering it via XML (see also http://beanvalidation.org/1.1/spec/#xml-mapping-constraintdefinition).