Java Code Examples for org.springframework.validation.beanvalidation.SpringValidatorAdapter

The following examples show how to use org.springframework.validation.beanvalidation.SpringValidatorAdapter. These examples are extracted from open source projects. 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 Project: liiklus   Source File: PropertiesUtil.java    License: 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 Project: chassis   Source File: RawWebSocketMessage.java    License: 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 Project: onetwo   Source File: ValidatorWrapper.java    License: Apache License 2.0 4 votes vote down vote up
private ValidatorWrapper(Validator validator) {
	super();
	this.validator = validator;
	this.adapter = new SpringValidatorAdapter(validator);
}
 
Example 4
Source Project: chassis   Source File: HttpValidator.java    License: Apache License 2.0 4 votes vote down vote up
@InitBinder
public void initBinder(WebDataBinder binder) {
	binder.setValidator(new SpringValidatorAdapter(messageValidator));
}