From 34ffd9654d5ab4ada14fb3b34792d7019a1a34f4 Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Wed, 20 Dec 2017 16:15:41 -0500 Subject: [PATCH] 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()` --- .../json/SimpleJsonSerializer.java | 19 +++++++++++++++---- .../json/SimpleJsonSerializerTests.java | 6 ++++++ 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/json/SimpleJsonSerializer.java b/spring-integration-core/src/main/java/org/springframework/integration/json/SimpleJsonSerializer.java index 610292d2bb..f4ad04e015 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/json/SimpleJsonSerializer.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/json/SimpleJsonSerializer.java @@ -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()) + "\""; } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/json/SimpleJsonSerializerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/json/SimpleJsonSerializerTests.java index f931b3feec..7e4c62f8ae 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/json/SimpleJsonSerializerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/json/SimpleJsonSerializerTests.java @@ -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"); + } + } }