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:
@@ -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()) + "\"";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user