Skip to content

Commit 34ffd96

Browse files
committed
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()`
1 parent e585bb9 commit 34ffd96

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

spring-integration-core/src/main/java/org/springframework/integration/json/SimpleJsonSerializer.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
* properties accessed by getters.
3434
*
3535
* @author Gary Russell
36+
* @author Artem Bilan
37+
*
3638
* @since 5.0
3739
*
3840
*/
@@ -66,10 +68,19 @@ public static String toJson(Object bean, String... propertiesToExclude) {
6668
result = readMethod.invoke(bean, emptyArgs);
6769
}
6870
catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
71+
Throwable exception = e;
72+
if (e instanceof InvocationTargetException) {
73+
exception = e.getCause();
74+
}
75+
6976
if (logger.isDebugEnabled()) {
70-
logger.debug("Failed to serialize property " + propertyName, e);
77+
logger.debug("Failed to serialize property " + propertyName, exception);
7178
}
72-
result = e.getMessage();
79+
80+
result =
81+
exception.getMessage() != null
82+
? exception.getMessage()
83+
: exception.toString();
7384
}
7485
stringBuilder.append(toElement(result)).append(",");
7586
}
@@ -89,7 +100,7 @@ private static String toElement(Object result) {
89100
return result.toString();
90101
}
91102
else {
92-
return "\"" + result.toString() + "\"";
103+
return "\"" + (result == null ? "null" : result.toString()) + "\"";
93104
}
94105
}
95106

spring-integration-core/src/test/java/org/springframework/integration/json/SimpleJsonSerializerTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
/**
2828
* @author Gary Russell
29+
* @author Artem Bilan
30+
*
2931
* @since 5.0
3032
*
3133
*/
@@ -83,6 +85,10 @@ public String fileInfo() {
8385
return this.fileInfo;
8486
}
8587

88+
public String getPermissions() {
89+
throw new UnsupportedOperationException("Permissions are not supported");
90+
}
91+
8692
}
8793

8894
}

0 commit comments

Comments
 (0)