Search in sources :

Example 1 with MethodArgumentNotValidException

use of org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException in project spring-framework by spring-projects.

the class PayloadMethodArgumentResolver method getValidator.

@Nullable
private Consumer<Object> getValidator(Message<?> message, MethodParameter parameter) {
    if (this.validator == null) {
        return null;
    }
    for (Annotation ann : parameter.getParameterAnnotations()) {
        Validated validatedAnn = AnnotationUtils.getAnnotation(ann, Validated.class);
        if (validatedAnn != null || ann.annotationType().getSimpleName().startsWith("Valid")) {
            Object hints = (validatedAnn != null ? validatedAnn.value() : AnnotationUtils.getValue(ann));
            Object[] validationHints = (hints instanceof Object[] ? (Object[]) hints : new Object[] { hints });
            String name = Conventions.getVariableNameForParameter(parameter);
            return target -> {
                BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(target, name);
                if (!ObjectUtils.isEmpty(validationHints) && this.validator instanceof SmartValidator) {
                    ((SmartValidator) this.validator).validate(target, bindingResult, validationHints);
                } else {
                    this.validator.validate(target, bindingResult);
                }
                if (bindingResult.hasErrors()) {
                    throw new MethodArgumentNotValidException(message, parameter, bindingResult);
                }
            };
        }
    }
    return null;
}
Also used : Decoder(org.springframework.core.codec.Decoder) Validator(org.springframework.validation.Validator) MethodArgumentNotValidException(org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException) Conventions(org.springframework.core.Conventions) DecodingException(org.springframework.core.codec.DecodingException) ArrayList(java.util.ArrayList) MimeType(org.springframework.util.MimeType) HandlerMethodArgumentResolver(org.springframework.messaging.handler.invocation.reactive.HandlerMethodArgumentResolver) Map(java.util.Map) MethodParameter(org.springframework.core.MethodParameter) DataBufferUtils(org.springframework.core.io.buffer.DataBufferUtils) Nullable(org.springframework.lang.Nullable) Message(org.springframework.messaging.Message) ResolvableType(org.springframework.core.ResolvableType) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ReactiveAdapterRegistry(org.springframework.core.ReactiveAdapterRegistry) ReactiveAdapter(org.springframework.core.ReactiveAdapter) Validated(org.springframework.validation.annotation.Validated) Publisher(org.reactivestreams.Publisher) AnnotationUtils(org.springframework.core.annotation.AnnotationUtils) ObjectUtils(org.springframework.util.ObjectUtils) Mono(reactor.core.publisher.Mono) MimeTypeUtils(org.springframework.util.MimeTypeUtils) DataBuffer(org.springframework.core.io.buffer.DataBuffer) MessageHeaders(org.springframework.messaging.MessageHeaders) MethodArgumentResolutionException(org.springframework.messaging.handler.invocation.MethodArgumentResolutionException) Consumer(java.util.function.Consumer) Flux(reactor.core.publisher.Flux) List(java.util.List) CollectionUtils(org.springframework.util.CollectionUtils) SmartValidator(org.springframework.validation.SmartValidator) Annotation(java.lang.annotation.Annotation) Payload(org.springframework.messaging.handler.annotation.Payload) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) Collections(java.util.Collections) Assert(org.springframework.util.Assert) StringUtils(org.springframework.util.StringUtils) Validated(org.springframework.validation.annotation.Validated) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SmartValidator(org.springframework.validation.SmartValidator) MethodArgumentNotValidException(org.springframework.messaging.handler.annotation.support.MethodArgumentNotValidException) Annotation(java.lang.annotation.Annotation) Nullable(org.springframework.lang.Nullable)

Aggregations

Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Consumer (java.util.function.Consumer)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 Publisher (org.reactivestreams.Publisher)1 Conventions (org.springframework.core.Conventions)1 MethodParameter (org.springframework.core.MethodParameter)1 ReactiveAdapter (org.springframework.core.ReactiveAdapter)1 ReactiveAdapterRegistry (org.springframework.core.ReactiveAdapterRegistry)1 ResolvableType (org.springframework.core.ResolvableType)1 AnnotationUtils (org.springframework.core.annotation.AnnotationUtils)1 Decoder (org.springframework.core.codec.Decoder)1 DecodingException (org.springframework.core.codec.DecodingException)1 DataBuffer (org.springframework.core.io.buffer.DataBuffer)1 DataBufferUtils (org.springframework.core.io.buffer.DataBufferUtils)1 Nullable (org.springframework.lang.Nullable)1