mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Restructure /configprops output
/configprops output now contains the prefix from @ConfigurationProperties as wells as bean name and actual properties fixes #267
This commit is contained in:
parent
0ecf7ed2a5
commit
07238b1ce8
@ -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<String, Object> invoke() {
|
||||
Map<String, Object> beans = extract(this.context);
|
||||
return beans;
|
||||
return extract(this.context);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Map<String, Object> extract(ApplicationContext context) {
|
||||
|
||||
Map<String, Object> result = new HashMap<String, Object>();
|
||||
Map<String, Object> beans = context
|
||||
.getBeansWithAnnotation(ConfigurationProperties.class);
|
||||
|
||||
// Serialize beans into map structure and sanitize values
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
for (Map.Entry<String, Object> entry : beans.entrySet()) {
|
||||
Map<String, Object> value = mapper.convertValue(entry.getValue(), Map.class);
|
||||
beans.put(entry.getKey(), sanitize(value));
|
||||
String beanName = entry.getKey();
|
||||
Object bean = entry.getValue();
|
||||
|
||||
Map<String, Object> root = new HashMap<String, Object>();
|
||||
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")
|
||||
|
@ -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<String, Object> properties = report.invoke();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) 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<String, Object> properties = report.invoke();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) 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<String, Object> properties = report.invoke();
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) properties
|
||||
.get("testProperties");
|
||||
Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) properties
|
||||
.get("testProperties")).get("properties");
|
||||
assertNotNull(nestedProperties);
|
||||
assertEquals("123456", nestedProperties.get("dbPassword"));
|
||||
assertEquals("******", nestedProperties.get("myTestProperty"));
|
||||
|
Loading…
Reference in New Issue
Block a user