Search in sources :

Example 1 with MethodArgumentResolutionException

use of org.springframework.messaging.handler.invocation.MethodArgumentResolutionException in project spring-framework by spring-projects.

the class PayloadMethodArgumentResolver method decodeContent.

private Mono<Object> decodeContent(MethodParameter parameter, Message<?> message, boolean isContentRequired, Flux<DataBuffer> content, MimeType mimeType) {
    ResolvableType targetType = ResolvableType.forMethodParameter(parameter);
    Class<?> resolvedType = targetType.resolve();
    ReactiveAdapter adapter = (resolvedType != null ? getAdapterRegistry().getAdapter(resolvedType) : null);
    ResolvableType elementType = (adapter != null ? targetType.getGeneric() : targetType);
    isContentRequired = isContentRequired || (adapter != null && !adapter.supportsEmpty());
    Consumer<Object> validator = getValidator(message, parameter);
    Map<String, Object> hints = Collections.emptyMap();
    for (Decoder<?> decoder : this.decoders) {
        if (decoder.canDecode(elementType, mimeType)) {
            if (adapter != null && adapter.isMultiValue()) {
                Flux<?> flux = content.filter(this::nonEmptyDataBuffer).map(buffer -> decoder.decode(buffer, elementType, mimeType, hints)).onErrorResume(ex -> Flux.error(handleReadError(parameter, message, ex)));
                if (isContentRequired) {
                    flux = flux.switchIfEmpty(Flux.error(() -> handleMissingBody(parameter, message)));
                }
                if (validator != null) {
                    flux = flux.doOnNext(validator);
                }
                return Mono.just(adapter.fromPublisher(flux));
            } else {
                // Single-value (with or without reactive type wrapper)
                Mono<?> mono = content.next().filter(this::nonEmptyDataBuffer).map(buffer -> decoder.decode(buffer, elementType, mimeType, hints)).onErrorResume(ex -> Mono.error(handleReadError(parameter, message, ex)));
                if (isContentRequired) {
                    mono = mono.switchIfEmpty(Mono.error(() -> handleMissingBody(parameter, message)));
                }
                if (validator != null) {
                    mono = mono.doOnNext(validator);
                }
                return (adapter != null ? Mono.just(adapter.fromPublisher(mono)) : Mono.from(mono));
            }
        }
    }
    return Mono.error(new MethodArgumentResolutionException(message, parameter, "Cannot decode to [" + targetType + "]" + message));
}
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) MethodArgumentResolutionException(org.springframework.messaging.handler.invocation.MethodArgumentResolutionException) ResolvableType(org.springframework.core.ResolvableType) ReactiveAdapter(org.springframework.core.ReactiveAdapter)

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