Throw a proper exception if no convert is found

Prior to this commit, no exception was raised if a message could not
be converted to the requested payload because no suitable converter
were found.

This commit adds an explicit check if the converted payload is null.

Issue: SPR-11817
This commit is contained in:
Stephane Nicoll
2014-05-28 16:48:48 +02:00
parent 8dec1db914
commit 12a9df8a1c
4 changed files with 53 additions and 6 deletions

View File

@@ -17,6 +17,8 @@
package org.springframework.messaging.core;
import org.springframework.messaging.Message;
import org.springframework.messaging.converter.MessageConversionException;
import org.springframework.messaging.converter.MessageConverter;
/**
* An extension of {@link AbstractMessageSendingTemplate} that adds support for
@@ -53,11 +55,23 @@ public abstract class AbstractMessageReceivingTemplate<D> extends AbstractMessag
public <T> T receiveAndConvert(D destination, Class<T> targetClass) {
Message<?> message = doReceive(destination);
if (message != null) {
return (T) getMessageConverter().fromMessage(message, targetClass);
return doConvert(message, targetClass);
}
else {
return null;
}
}
@SuppressWarnings("unchecked")
protected <T> T doConvert(Message<?> message, Class<T> targetClass) {
MessageConverter messageConverter = getMessageConverter();
T value = (T) messageConverter.fromMessage(message, targetClass);
if (value == null) {
throw new MessageConversionException("Unable to convert payload='"
+ message.getPayload() + "' to type='" + targetClass
+ "', converter=[" + messageConverter + "]");
}
return value;
}
}