From 07238b1ce8c7a5e77f86a47825809cc326fb42af Mon Sep 17 00:00:00 2001 From: Christian Dupuis Date: Tue, 28 Jan 2014 14:41:24 +0100 Subject: [PATCH] Restructure /configprops output /configprops output now contains the prefix from @ConfigurationProperties as wells as bean name and actual properties fixes #267 --- ...ConfigurationPropertiesReportEndpoint.java | 27 ++++++++++++++----- ...gurationPropertiesReportEndpointTests.java | 19 ++++++++++--- 2 files changed, 36 insertions(+), 10 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpoint.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpoint.java index 00fae013a1..02ff5b5fa3 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpoint.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpoint.java @@ -16,13 +16,16 @@ package org.springframework.boot.actuate.endpoint; +import java.util.HashMap; import java.util.Map; import org.springframework.beans.BeansException; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; +import org.springframework.core.annotation.AnnotationUtils; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; import com.fasterxml.jackson.databind.ObjectMapper; @@ -65,28 +68,40 @@ public class ConfigurationPropertiesReportEndpoint extends @Override public Map invoke() { - Map beans = extract(this.context); - return beans; + return extract(this.context); } @SuppressWarnings("unchecked") private Map extract(ApplicationContext context) { + Map result = new HashMap(); Map beans = context .getBeansWithAnnotation(ConfigurationProperties.class); // Serialize beans into map structure and sanitize values ObjectMapper mapper = new ObjectMapper(); for (Map.Entry entry : beans.entrySet()) { - Map value = mapper.convertValue(entry.getValue(), Map.class); - beans.put(entry.getKey(), sanitize(value)); + String beanName = entry.getKey(); + Object bean = entry.getValue(); + + Map root = new HashMap(); + root.put("prefix", extractPrefix(bean)); + root.put("properties", sanitize(mapper.convertValue(bean, Map.class))); + result.put(beanName, root); } if (context.getParent() != null) { - beans.put("parent", extract(context.getParent())); + result.put("parent", extract(context.getParent())); } - return beans; + return result; + } + + private String extractPrefix(Object bean) { + ConfigurationProperties annotation = AnnotationUtils.findAnnotation( + bean.getClass(), ConfigurationProperties.class); + return (StringUtils.hasLength(annotation.value()) ? annotation.value() + : annotation.name()); } @SuppressWarnings("unchecked") diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointTests.java index 4d77d52713..62274bf015 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ConfigurationPropertiesReportEndpointTests.java @@ -44,13 +44,24 @@ public class ConfigurationPropertiesReportEndpointTests extends @Test @SuppressWarnings("unchecked") - public void testDefaultKeySanitization() throws Exception { + public void testNaming() throws Exception { ConfigurationPropertiesReportEndpoint report = getEndpointBean(); - // report.setKeysToSanitize(new String[] {}); Map properties = report.invoke(); Map nestedProperties = (Map) properties .get("testProperties"); assertNotNull(nestedProperties); + assertEquals("test", nestedProperties.get("prefix")); + assertNotNull(nestedProperties.get("properties")); + } + + @Test + @SuppressWarnings("unchecked") + public void testDefaultKeySanitization() throws Exception { + ConfigurationPropertiesReportEndpoint report = getEndpointBean(); + Map properties = report.invoke(); + Map nestedProperties = (Map) ((Map) properties + .get("testProperties")).get("properties"); + assertNotNull(nestedProperties); assertEquals("******", nestedProperties.get("dbPassword")); assertEquals("654321", nestedProperties.get("myTestProperty")); } @@ -61,8 +72,8 @@ public class ConfigurationPropertiesReportEndpointTests extends ConfigurationPropertiesReportEndpoint report = getEndpointBean(); report.setKeysToSanitize(new String[] { "property" }); Map properties = report.invoke(); - Map nestedProperties = (Map) properties - .get("testProperties"); + Map nestedProperties = (Map) ((Map) properties + .get("testProperties")).get("properties"); assertNotNull(nestedProperties); assertEquals("123456", nestedProperties.get("dbPassword")); assertEquals("******", nestedProperties.get("myTestProperty"));