Last active
April 20, 2019 08:38
-
-
Save pianovwork/feb4188719adcfd0bf248d0f113b4fdd to your computer and use it in GitHub Desktop.
Spring Validation Setup ~ CompositeValidator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Component | |
public class CompositeValidator implements SmartValidator { | |
@Autowired | |
private List<Validator> validators = Collections.emptyList(); | |
@PostConstruct | |
public void init() { | |
Collections.sort(validators, AnnotationAwareOrderComparator.INSTANCE); | |
} | |
@Override | |
public boolean supports(Class<?> clazz) { | |
for (Validator validator : validators) { | |
if (validator.supports(clazz)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
@Override | |
public void validate(Object target, Errors errors) { | |
validate(target, errors, javax.validation.groups.Default.class); | |
} | |
@Override | |
public void validate(Object target, Errors errors, Object... validationHints) { | |
Class<?> clazz = target.getClass(); | |
for (Validator validator : validators) { | |
if (validator.supports(clazz)) { | |
if (validator instanceof SmartValidator) { | |
((SmartValidator) validator).validate(target, errors, validationHints); | |
} else { | |
validator.validate(target, errors); | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
@RequestMapping("/my/resources") | |
public class SomeController { | |
@RequestMapping(method = RequestMethod.POST) | |
public Object save( | |
@Validated(javax.validation.groups.Default.class) // this interface descriptor (class) is used by default | |
@RequestBody MyResource myResource | |
) { return null; } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class WebConfig { | |
/** used for Annotation based validation, it can be created by spring automaticaly and you don't do it manualy */ | |
// @Bean | |
// public Validator jsr303Validator() { | |
// LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); | |
// // validator.setValidationMessageSource(...); | |
// return validator; | |
// } | |
@Bean | |
public WebMvcConfigurerAdapter webMvcConfigurerAdapter() { | |
return new WebMvcConfigurerAdapter() { | |
@Autowired | |
private CompositeValidator validator; | |
@Override | |
public Validator getValidator() { | |
return validator; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- used for Annotation based validation, it can be created by spring automaticaly and you don't do it manualy --> | |
<!--<bean id="jsr303Validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">--> | |
<!-- <property name="validationMessageSource" ref="messageSource"/>--> | |
<!--</bean>--> | |
<mvc:annotation-driven validator="compositeValidator"> | |
//... | |
</mvc:annotation-driven> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment