Search in sources :

Example 1 with MessageConversionException

use of org.springframework.messaging.converter.MessageConversionException 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 MessageConversionException

use of org.springframework.messaging.converter.MessageConversionException 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 MessageConversionException

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

the class DefaultStompSession method createMessage.

@SuppressWarnings("unchecked")
private Message<byte[]> createMessage(StompHeaderAccessor accessor, Object payload) {
    accessor.updateSimpMessageHeadersFromStompHeaders();
    Message<byte[]> message;
    if (payload == null) {
        message = MessageBuilder.createMessage(EMPTY_PAYLOAD, accessor.getMessageHeaders());
    } else if (payload instanceof byte[]) {
        message = MessageBuilder.createMessage((byte[]) payload, accessor.getMessageHeaders());
    } else {
        message = (Message<byte[]>) getMessageConverter().toMessage(payload, accessor.getMessageHeaders());
        accessor.updateStompHeadersFromSimpMessageHeaders();
        if (message == null) {
            throw new MessageConversionException("Unable to convert payload with type='" + payload.getClass().getName() + "', contentType='" + accessor.getContentType() + "', converter=[" + getMessageConverter() + "]");
        }
    }
    return message;
}
Also used : MessageConversionException(org.springframework.messaging.converter.MessageConversionException) Message(org.springframework.messaging.Message)

Example 4 with MessageConversionException

use of org.springframework.messaging.converter.MessageConversionException in project spring-integration by spring-projects.

the class DefaultPahoMessageConverter method messageToMqttBytes.

/**
 * Subclasses can override this method to convert the payload to a byte[].
 * The default implementation accepts a byte[] or String payload.
 * If a {@link BytesMessageMapper} is provided, conversion to byte[]
 * is delegated to it, so any payload that it can handle is supported.
 *
 * @param message The outbound Message.
 * @return The byte[] which will become the payload of the MQTT Message.
 */
protected byte[] messageToMqttBytes(Message<?> message) {
    if (this.bytesMessageMapper != null) {
        try {
            return this.bytesMessageMapper.fromMessage(message);
        } catch (Exception e) {
            throw new MessageHandlingException(message, "Failed to map outbound message", e);
        }
    } else {
        Object payload = message.getPayload();
        Assert.isTrue(payload instanceof byte[] || payload instanceof String, () -> "This default converter can only handle 'byte[]' or 'String' payloads; consider adding a " + "transformer to your flow definition, or provide a BytesMessageMapper, " + "or subclass this converter for " + payload.getClass().getName() + " payloads");
        byte[] payloadBytes;
        if (payload instanceof String) {
            try {
                payloadBytes = ((String) payload).getBytes(this.charset);
            } catch (Exception e) {
                throw new MessageConversionException("failed to convert Message to object", e);
            }
        } else {
            payloadBytes = (byte[]) payload;
        }
        return payloadBytes;
    }
}
Also used : MessageConversionException(org.springframework.messaging.converter.MessageConversionException) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 5 with MessageConversionException

use of org.springframework.messaging.converter.MessageConversionException in project spring-integration by spring-projects.

the class XPathMessageSplitter method splitMessage.

@Override
protected Object splitMessage(Message<?> message) {
    try {
        Object payload = message.getPayload();
        Object result = null;
        if (payload instanceof Node) {
            result = splitNode((Node) payload);
        } else {
            Document document = this.xmlPayloadConverter.convertToDocument(payload);
            Assert.notNull(document, "unsupported payload type [" + payload.getClass().getName() + "]");
            result = splitDocument(document);
        }
        return result;
    } catch (ParserConfigurationException e) {
        throw new MessageConversionException(message, "failed to create DocumentBuilder", e);
    } catch (Exception e) {
        throw new MessageHandlingException(message, "failed to split Message payload", e);
    }
}
Also used : MessageConversionException(org.springframework.messaging.converter.MessageConversionException) Node(org.w3c.dom.Node) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) XPathExpressionException(javax.xml.xpath.XPathExpressionException) TransformerException(javax.xml.transform.TransformerException) XPathException(org.springframework.xml.xpath.XPathException) MessageHandlingException(org.springframework.messaging.MessageHandlingException) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Aggregations

MessageConversionException (org.springframework.messaging.converter.MessageConversionException)16 IOException (java.io.IOException)3 Test (org.junit.Test)3 MessageHandlingException (org.springframework.messaging.MessageHandlingException)3 MessageConverter (org.springframework.messaging.converter.MessageConverter)3 SmartMessageConverter (org.springframework.messaging.converter.SmartMessageConverter)3 MimeType (org.springframework.util.MimeType)3 Schema (org.apache.avro.Schema)2 SpringApplicationBuilder (org.springframework.boot.builder.SpringApplicationBuilder)2 InputDestination (org.springframework.cloud.stream.binder.test.InputDestination)2 TestChannelBinder (org.springframework.cloud.stream.binder.test.TestChannelBinder)2 ApplicationContext (org.springframework.context.ApplicationContext)2 MessageHeaders (org.springframework.messaging.MessageHeaders)2 JavaType (com.fasterxml.jackson.databind.JavaType)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 Type (java.lang.reflect.Type)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 TransformerException (javax.xml.transform.TransformerException)1 XPathExpressionException (javax.xml.xpath.XPathExpressionException)1