Search in sources :

Example 11 with MessageConversionException

use of org.springframework.messaging.converter.MessageConversionException in project spring-cloud-stream by spring-cloud.

the class ContentTypeTckTests method _jsonToPojoWrongDefaultContentTypeProperty.

// Failure tests
@Test
public void _jsonToPojoWrongDefaultContentTypeProperty() {
    ApplicationContext context = new SpringApplicationBuilder(PojoToPojoStreamListener.class).web(WebApplicationType.NONE).run("--spring.cloud.stream.default.contentType=text/plain", "--spring.jmx.enabled=false");
    InputDestination source = context.getBean(InputDestination.class);
    TestChannelBinder binder = context.getBean(TestChannelBinder.class);
    String jsonPayload = "{\"name\":\"oleg\"}";
    source.send(new GenericMessage<>(jsonPayload.getBytes()));
    assertTrue(binder.getLastError().getPayload() instanceof MessageConversionException);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) InputDestination(org.springframework.cloud.stream.binder.test.InputDestination) Test(org.junit.Test)

Example 12 with MessageConversionException

use of org.springframework.messaging.converter.MessageConversionException in project spring-cloud-stream by spring-cloud.

the class ContentTypeTckTests method _toStringDefaultContentTypePropertyUnknownContentType.

@Test
public void _toStringDefaultContentTypePropertyUnknownContentType() {
    ApplicationContext context = new SpringApplicationBuilder(StringToStringStreamListener.class).web(WebApplicationType.NONE).run("--spring.cloud.stream.default.contentType=foo/bar", "--spring.jmx.enabled=false");
    InputDestination source = context.getBean(InputDestination.class);
    TestChannelBinder binder = context.getBean(TestChannelBinder.class);
    String jsonPayload = "{\"name\":\"oleg\"}";
    source.send(new GenericMessage<>(jsonPayload.getBytes()));
    assertTrue(binder.getLastError().getPayload() instanceof MessageConversionException);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) SpringApplicationBuilder(org.springframework.boot.builder.SpringApplicationBuilder) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) InputDestination(org.springframework.cloud.stream.binder.test.InputDestination) Test(org.junit.Test)

Example 13 with MessageConversionException

use of org.springframework.messaging.converter.MessageConversionException in project spring-cloud-stream by spring-cloud.

the class ApplicationJsonMessageMarshallingConverter method convertParameterizedType.

private Object convertParameterizedType(Message<?> message, Class<?> targetClass, ParameterizedTypeReference<?> conversionHint) {
    ObjectMapper objectMapper = this.getObjectMapper();
    Object payload = message.getPayload();
    try {
        JavaType type = this.typeCache.get(conversionHint);
        if (type == null) {
            type = objectMapper.getTypeFactory().constructType((conversionHint).getType());
            this.typeCache.put(conversionHint, type);
        }
        if (payload instanceof byte[]) {
            return objectMapper.readValue((byte[]) payload, type);
        } else if (payload instanceof String) {
            return objectMapper.readValue((String) payload, type);
        } else {
            return null;
        }
    } catch (IOException e) {
        throw new MessageConversionException("Cannot parse payload ", e);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 14 with MessageConversionException

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

the class DefaultPahoMessageConverter method toMessage.

@SuppressWarnings("unchecked")
@Override
public Message<?> toMessage(String topic, MqttMessage mqttMessage) {
    try {
        AbstractIntegrationMessageBuilder<Object> messageBuilder;
        if (this.bytesMessageMapper != null) {
            messageBuilder = (AbstractIntegrationMessageBuilder<Object>) getMessageBuilderFactory().fromMessage(this.bytesMessageMapper.toMessage(mqttMessage.getPayload()));
        } else {
            messageBuilder = getMessageBuilderFactory().withPayload(mqttBytesToPayload(mqttMessage));
        }
        messageBuilder.setHeader(MqttHeaders.RECEIVED_QOS, mqttMessage.getQos()).setHeader(MqttHeaders.DUPLICATE, mqttMessage.isDuplicate()).setHeader(MqttHeaders.RECEIVED_RETAINED, mqttMessage.isRetained());
        if (topic != null) {
            messageBuilder.setHeader(MqttHeaders.RECEIVED_TOPIC, topic);
        }
        return messageBuilder.build();
    } catch (Exception e) {
        throw new MessageConversionException("failed to convert object to Message", e);
    }
}
Also used : MessageConversionException(org.springframework.messaging.converter.MessageConversionException) MessageConversionException(org.springframework.messaging.converter.MessageConversionException) MessageHandlingException(org.springframework.messaging.MessageHandlingException)

Example 15 with MessageConversionException

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