Fix SimpleJsonSerializer when exception on prop

When the target Java Bean reader throws an exception it is wrapped to
the `InvocationTargetException` which `getMessage()` returns `null`.

* Extract the `cause` when `InvocationTargetException` and check the
`message` for null anyway.
* Also check for `null` before calling `toString()` in the
`SimpleJsonSerializer.toElement()`
This commit is contained in:
Artem Bilan
2017-12-20 16:15:41 -05:00
parent e585bb949f
commit 34ffd9654d
2 changed files with 21 additions and 4 deletions

View File

@@ -33,6 +33,8 @@ import org.springframework.beans.BeanUtils;
* properties accessed by getters.
*
* @author Gary Russell
* @author Artem Bilan
*
* @since 5.0
*
*/
@@ -66,10 +68,19 @@ public final class SimpleJsonSerializer {
result = readMethod.invoke(bean, emptyArgs);
}
catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to serialize property " + propertyName, e);
Throwable exception = e;
if (e instanceof InvocationTargetException) {
exception = e.getCause();
}
result = e.getMessage();
if (logger.isDebugEnabled()) {
logger.debug("Failed to serialize property " + propertyName, exception);
}
result =
exception.getMessage() != null
? exception.getMessage()
: exception.toString();
}
stringBuilder.append(toElement(result)).append(",");
}
@@ -89,7 +100,7 @@ public final class SimpleJsonSerializer {
return result.toString();
}
else {
return "\"" + result.toString() + "\"";
return "\"" + (result == null ? "null" : result.toString()) + "\"";
}
}

View File

@@ -26,6 +26,8 @@ import org.springframework.integration.support.json.JsonObjectMapperProvider;
/**
* @author Gary Russell
* @author Artem Bilan
*
* @since 5.0
*
*/
@@ -83,6 +85,10 @@ public class SimpleJsonSerializerTests {
return this.fileInfo;
}
public String getPermissions() {
throw new UnsupportedOperationException("Permissions are not supported");
}
}
}