All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.smsc.config.SpringDataRestValidationConfiguration Maven / Gradle / Ivy

package io.smsc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
import org.springframework.validation.Validator;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;

/**
 * The SpringDataRestValidationConfiguration class is used for customization
 * hibernate bean validation to launch before entity is created or updated
 *
 * @author Nazar Lipkovskyy
 * @since 0.0.1-SNAPSHOT
 */
@Configuration
public class SpringDataRestValidationConfiguration extends RepositoryRestConfigurerAdapter {

    /**
     * Create a validator to use in bean validation - primary to be able
     * to autowire without qualifier
     *
     * @return validator
     */
    @Bean
    @Primary
    Validator validator() {
        return new LocalValidatorFactoryBean();
    }

    @Override
    public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
        Validator validator = validator();
        //bean validation always before save and create
        validatingListener.addValidator("beforeCreate", validator);
        validatingListener.addValidator("beforeSave", validator);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy