Search in sources :

Example 1 with SmartMessageConverter

use of org.springframework.messaging.converter.SmartMessageConverter in project spring-framework by spring-projects.

the class MessageMethodArgumentResolver method convertPayload.

private Object convertPayload(Message<?> message, MethodParameter parameter, Class<?> targetPayloadType) {
    Object result = null;
    if (this.converter instanceof SmartMessageConverter) {
        SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter;
        result = smartConverter.fromMessage(message, targetPayloadType, parameter);
    } else if (this.converter != null) {
        result = this.converter.fromMessage(message, targetPayloadType);
    }
    if (result == null) {
        throw new MessageConversionException(message, "No converter found from actual payload type '" + ClassUtils.getDescriptiveType(message.getPayload()) + "' to expected payload type '" + ClassUtils.getQualifiedName(targetPayloadType) + "'");
    }
    return result;
}
Also used : SmartMessageConverter(org.springframework.messaging.converter.SmartMessageConverter) MessageConversionException(org.springframework.messaging.converter.MessageConversionException)

Example 2 with SmartMessageConverter

use of org.springframework.messaging.converter.SmartMessageConverter in project spring-framework by spring-projects.

the class PayloadArgumentResolver method resolveArgument.

@Override
public Object resolveArgument(MethodParameter parameter, Message<?> message) throws Exception {
    Payload ann = parameter.getParameterAnnotation(Payload.class);
    if (ann != null && StringUtils.hasText(ann.expression())) {
        throw new IllegalStateException("@Payload SpEL expressions not supported by this resolver");
    }
    Object payload = message.getPayload();
    if (isEmptyPayload(payload)) {
        if (ann == null || ann.required()) {
            String paramName = getParameterName(parameter);
            BindingResult bindingResult = new BeanPropertyBindingResult(payload, paramName);
            bindingResult.addError(new ObjectError(paramName, "Payload value must not be empty"));
            throw new MethodArgumentNotValidException(message, parameter, bindingResult);
        } else {
            return null;
        }
    }
    Class<?> targetClass = parameter.getParameterType();
    Class<?> payloadClass = payload.getClass();
    if (ClassUtils.isAssignable(targetClass, payloadClass)) {
        validate(message, parameter, payload);
        return payload;
    } else {
        if (this.converter instanceof SmartMessageConverter) {
            SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter;
            payload = smartConverter.fromMessage(message, targetClass, parameter);
        } else {
            payload = this.converter.fromMessage(message, targetClass);
        }
        if (payload == null) {
            throw new MessageConversionException(message, "Cannot convert from [" + payloadClass.getName() + "] to [" + targetClass.getName() + "] for " + message);
        }
        validate(message, parameter, payload);
        return payload;
    }
}
Also used : SmartMessageConverter(org.springframework.messaging.converter.SmartMessageConverter) BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ObjectError(org.springframework.validation.ObjectError) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) Payload(org.springframework.messaging.handler.annotation.Payload)

Example 3 with SmartMessageConverter

use of org.springframework.messaging.converter.SmartMessageConverter in project spring-framework by spring-projects.

the class AbstractMessageSendingTemplate method doConvert.

/**
 * Convert the given Object to serialized form, possibly using a
 * {@link MessageConverter}, wrap it as a message with the given
 * headers and apply the given post processor.
 * @param payload the Object to use as payload
 * @param headers the headers for the message to send
 * @param postProcessor the post processor to apply to the message
 * @return the converted message
 */
protected Message<?> doConvert(Object payload, @Nullable Map<String, Object> headers, @Nullable MessagePostProcessor postProcessor) {
    MessageHeaders messageHeaders = null;
    Object conversionHint = (headers != null ? headers.get(CONVERSION_HINT_HEADER) : null);
    Map<String, Object> headersToUse = processHeadersToSend(headers);
    if (headersToUse != null) {
        if (headersToUse instanceof MessageHeaders) {
            messageHeaders = (MessageHeaders) headersToUse;
        } else {
            messageHeaders = new MessageHeaders(headersToUse);
        }
    }
    MessageConverter converter = getMessageConverter();
    Message<?> message = (converter instanceof SmartMessageConverter ? ((SmartMessageConverter) converter).toMessage(payload, messageHeaders, conversionHint) : converter.toMessage(payload, messageHeaders));
    if (message == null) {
        String payloadType = payload.getClass().getName();
        Object contentType = (messageHeaders != null ? messageHeaders.get(MessageHeaders.CONTENT_TYPE) : null);
        throw new MessageConversionException("Unable to convert payload with type='" + payloadType + "', contentType='" + contentType + "', converter=[" + getMessageConverter() + "]");
    }
    if (postProcessor != null) {
        message = postProcessor.postProcessMessage(message);
    }
    return message;
}
Also used : SmartMessageConverter(org.springframework.messaging.converter.SmartMessageConverter) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) MessageHeaders(org.springframework.messaging.MessageHeaders) SmartMessageConverter(org.springframework.messaging.converter.SmartMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) SimpleMessageConverter(org.springframework.messaging.converter.SimpleMessageConverter)

Aggregations

MessageConversionException (org.springframework.messaging.converter.MessageConversionException)3 SmartMessageConverter (org.springframework.messaging.converter.SmartMessageConverter)3 MessageHeaders (org.springframework.messaging.MessageHeaders)1 MessageConverter (org.springframework.messaging.converter.MessageConverter)1 SimpleMessageConverter (org.springframework.messaging.converter.SimpleMessageConverter)1 Payload (org.springframework.messaging.handler.annotation.Payload)1 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)1 BindingResult (org.springframework.validation.BindingResult)1 ObjectError (org.springframework.validation.ObjectError)1