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);
}
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);
}
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);
}
}
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);
}
}
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;
}
Aggregations