org.springframework.validation.beanvalidation.SpringValidatorAdapter Java Examples

The following examples show how to use org.springframework.validation.beanvalidation.SpringValidatorAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PropertiesUtil.java    From liiklus with MIT License 6 votes vote down vote up
public static <T> T bind(ConfigurableEnvironment environment, @NonNull T properties) {
    var configurationProperties = AnnotationUtils.findAnnotation(properties.getClass(), ConfigurationProperties.class);

    if (configurationProperties == null) {
        throw new IllegalArgumentException(properties.getClass() + " Must be annotated with @ConfigurationProperties");
    }

    var property = configurationProperties.prefix();

    var validationBindHandler = new ValidationBindHandler(
            new SpringValidatorAdapter(Validation.buildDefaultValidatorFactory().getValidator())
    );

    var bindable = Bindable.ofInstance(properties);
    return Binder.get(environment).bind(property, bindable, validationBindHandler).orElseGet(bindable.getValue());
}
 
Example #2
Source File: RawWebSocketMessage.java    From chassis with Apache License 2.0 6 votes vote down vote up
/**
 * Deserializes the given message.
 * 
 * @param action
 * @return
 * @throws Exception
 */
public T deserialize(WebSocketAction action) throws Exception {
	// first deserialize
	T message = null;
	
	if (messageClass != null) {
		message = serDe.deserialize(new ByteBufferBackedInputStream(rawData), messageClass);
	}
	
	// then validate
	if (message != null && action.shouldValidatePayload()) {
		SpringValidatorAdapter validatorAdapter = new SpringValidatorAdapter(messageValidator);
		
		BeanPropertyBindingResult result = new BeanPropertyBindingResult(message, messageClass.getName());
		
		validatorAdapter.validate(message, result);
		
		if (result.hasErrors()) {
			throw new MethodArgumentNotValidException(new MethodParameter(action.getMethod(), action.getPayloadParameterIndex()), result);
		}
	}
	
	return message;
}
 
Example #3
Source File: HttpValidator.java    From chassis with Apache License 2.0 5 votes vote down vote up
@InitBinder
public void initBinder(WebDataBinder binder) {
	binder.setValidator(new SpringValidatorAdapter(messageValidator));
}
 
Example #4
Source File: ValidatorWrapper.java    From onetwo with Apache License 2.0 4 votes vote down vote up
private ValidatorWrapper(Validator validator) {
	super();
	this.validator = validator;
	this.adapter = new SpringValidatorAdapter(validator);
}