INT-4362: MMIH Catch Conversion Exception

JIRA: https://jira.spring.io/browse/INT-4362

INT-4312 added a JSON conversion when there is a single method with a non-String parameter.

Add more tests for parameters of type `Message`.

Catch conversion exceptions and use the message as-is if the conversion fails.
This commit is contained in:
Gary Russell
2017-11-13 09:35:02 -05:00
committed by Artem Bilan
parent 4207f36a17
commit 70c166fbfd
2 changed files with 108 additions and 14 deletions

View File

@@ -259,27 +259,38 @@ public class MessagingMethodInvokerHelper<T> extends AbstractExpressionEvaluator
@SuppressWarnings("unchecked")
public T process(Message<?> message) throws Exception {
Message<?> messageToProcess = message;
/*
* If there's a single method, the content is JSON, the payload is a
* String or byte[], the parameter doesn't match the payload,
* and there is a Json Object Mapper on the CP,
* convert.
*/
Message<?> messageToProcess = possiblyConvert(message);
ParametersWrapper parameters = new ParametersWrapper(messageToProcess);
return processInternal(parameters);
}
/*
* If there's a single method, the content is JSON, the payload is a
* String or byte[], the parameter doesn't match the payload,
* and there is a Json Object Mapper on the CP,
* convert.
*/
private Message<?> possiblyConvert(Message<?> message) throws Exception {
if (this.handlerMethod != null && this.handlerMethod.getTargetParameterType() != null &&
this.jsonObjectMapper != null) {
Class<?> type = this.handlerMethod.getTargetParameterType();
if ((message.getPayload() instanceof String && !type.equals(String.class)
|| message.getPayload() instanceof byte[] && !type.equals(byte[].class))
&& contentTypeIsJson(message)) {
messageToProcess = getMessageBuilderFactory()
.withPayload(this.jsonObjectMapper.fromJson(message.getPayload(), type))
.copyHeaders(message.getHeaders())
.build();
try {
return getMessageBuilderFactory()
.withPayload(this.jsonObjectMapper.fromJson(message.getPayload(), type))
.copyHeaders(message.getHeaders())
.build();
}
catch (Exception e) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to convert from JSON", e);
}
}
}
}
ParametersWrapper parameters = new ParametersWrapper(messageToProcess);
return processInternal(parameters);
return message;
}
private boolean contentTypeIsJson(Message<?> message) {