diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BindFailureAnalyzer.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BindFailureAnalyzer.java index d6cc897513..6c4f6e30e8 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BindFailureAnalyzer.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/BindFailureAnalyzer.java @@ -70,11 +70,12 @@ class BindFailureAnalyzer extends AbstractFailureAnalyzer { } private String getMessage(BindException cause) { - if (cause.getCause() != null - && StringUtils.hasText(cause.getCause().getMessage())) { - return cause.getCause().getMessage(); + Throwable failure = cause; + while (failure.getCause() != null) { + failure = failure.getCause(); } - return cause.getMessage(); + return (StringUtils.hasText(failure.getMessage()) ? failure.getMessage() + : cause.getMessage()); } private FailureAnalysis getFailureAnalysis(Object description, BindException cause) { diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BindFailureAnalyzerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BindFailureAnalyzerTests.java index ba2bbf665b..b3542a8575 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BindFailureAnalyzerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/BindFailureAnalyzerTests.java @@ -77,6 +77,14 @@ public class BindFailureAnalyzerTests { } } + @Test + public void bindExceptionWithNestedFailureShouldDisplayNestedMessage() { + FailureAnalysis analysis = performAnalysis(NestedFailureConfiguration.class, + "test.foo.value=hello"); + assertThat(analysis.getDescription()).contains(failure("test.foo.value", "hello", + "\"test.foo.value\" from property source \"test\"", "This is a failure")); + } + private static String failure(String property, String value, String origin, String reason) { return String.format( @@ -139,6 +147,11 @@ public class BindFailureAnalyzerTests { } + @EnableConfigurationProperties(NestedFailureProperties.class) + static class NestedFailureConfiguration { + + } + @ConfigurationProperties("test.foo") @Validated static class FieldValidationFailureProperties { @@ -201,6 +214,21 @@ public class BindFailureAnalyzerTests { } + @ConfigurationProperties("test.foo") + static class NestedFailureProperties { + + private String value; + + public String getValue() { + return this.value; + } + + public void setValue(String value) { + throw new RuntimeException("This is a failure"); + } + + } + enum Fruit { APPLE, BANANA, ORANGE