spring-projects / spring-data-rest

Simplifies building hypermedia-driven REST web services on top of Spring Data repositories
https://spring.io/projects/spring-data-rest
Apache License 2.0
919 stars 563 forks source link

Document how to enable validating entities with JSR-303 bean validation annotations [DATAREST-372] #706

Open spring-projects-issues opened 10 years ago

spring-projects-issues commented 10 years ago

Marcel Overdijk opened DATAREST-372 and commented

The Spring Data REST homepage states:

However in the documentation I cannot find back how this should be enabled. By default entities with JSR-303 annotation are not validated.

What I did to make it working is:

@Configuration
protected static class CustomRepositoryRestMvcConfiguration extends RepositoryRestMvcConfiguration {

    @Autowired
    private Validator validator;

    @Override
    protected void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
        validatingListener.addValidator("beforeCreate", validator);
        validatingListener.addValidator("beforeSave", validator);
    }
}

I don't know if this the best way to enable JSR-303 bean validation but either way I think it's good to add to the documentation how to enable it


Affects: 2.1.2 (Dijkstra SR2)

6 votes, 8 watchers

spring-projects-issues commented 10 years ago

Marcel Overdijk commented

See also discussion: http://stackoverflow.com/questions/25220440/can-jsr-303-bean-validation-be-used-with-spring-data-rest