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