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