Skip to content

Commit 78061f4

Browse files
committed
Merge pull request #24718 from bono007
* pr/24718: Polish "Filter properties with a particular prefix" Filter properties with a particular prefix Closes gh-24718
2 parents 0f9fb13 + b92bb93 commit 78061f4

File tree

7 files changed

+399
-14
lines changed

7 files changed

+399
-14
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/docs/asciidoc/endpoints/configprops.adoc

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ The `configprops` endpoint provides information about the application's `@Config
66

77

88
[[configprops-retrieving]]
9-
== Retrieving the @ConfigurationProperties Bean
9+
== Retrieving All @ConfigurationProperties Beans
1010

11-
To retrieve the `@ConfigurationProperties` beans, make a `GET` request to `/actuator/configprops`, as shown in the following curl-based example:
11+
To retrieve all of the `@ConfigurationProperties` beans, make a `GET` request to `/actuator/configprops`, as shown in the following curl-based example:
1212

13-
include::{snippets}/configprops/curl-request.adoc[]
13+
include::{snippets}/configprops/all/curl-request.adoc[]
1414

1515
The resulting response is similar to the following:
1616

17-
include::{snippets}/configprops/http-response.adoc[]
17+
include::{snippets}/configprops/all/http-response.adoc[]
1818

1919

2020

@@ -25,4 +25,30 @@ The response contains details of the application's `@ConfigurationProperties` be
2525
The following table describes the structure of the response:
2626

2727
[cols="2,1,3"]
28-
include::{snippets}/configprops/response-fields.adoc[]
28+
include::{snippets}/configprops/all/response-fields.adoc[]
29+
30+
31+
32+
[[configprops-retrieving-by-prefix]]
33+
== Retrieving @ConfigurationProperties Beans By Prefix
34+
35+
To retrieve the `@ConfigurationProperties` beans mapped under a certain prefix, make a `GET` request to `/actuator/configprops/\{prefix}`, as shown in the following curl-based example:
36+
37+
include::{snippets}/configprops/prefixed/curl-request.adoc[]
38+
39+
The resulting response is similar to the following:
40+
41+
include::{snippets}/configprops/prefixed/http-response.adoc[]
42+
43+
NOTE: The `\{prefix}` does not need to be exact, a more general prefix will return all beans mapped under that prefix stem.
44+
45+
46+
47+
[[configprops-retrieving-by-prefix-response-structure]]
48+
=== Response Structure
49+
50+
The response contains details of the application's `@ConfigurationProperties` beans.
51+
The following table describes the structure of the response:
52+
53+
[cols="2,1,3"]
54+
include::{snippets}/configprops/prefixed/response-fields.adoc[]

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/context/properties/ConfigurationPropertiesReportEndpointAutoConfiguration.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,7 +18,9 @@
1818

1919
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
2020
import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint;
21+
import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpointWebExtension;
2122
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2224
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2325
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2426
import org.springframework.context.annotation.Bean;
@@ -30,6 +32,7 @@
3032
*
3133
* @author Phillip Webb
3234
* @author Stephane Nicoll
35+
* @author Chris Bono
3336
* @since 2.0.0
3437
*/
3538
@Configuration(proxyBeanMethods = false)
@@ -49,4 +52,12 @@ public ConfigurationPropertiesReportEndpoint configurationPropertiesReportEndpoi
4952
return endpoint;
5053
}
5154

55+
@Bean
56+
@ConditionalOnMissingBean
57+
@ConditionalOnBean(ConfigurationPropertiesReportEndpoint.class)
58+
public ConfigurationPropertiesReportEndpointWebExtension configurationPropertiesReportEndpointWebExtension(
59+
ConfigurationPropertiesReportEndpoint configurationPropertiesReportEndpoint) {
60+
return new ConfigurationPropertiesReportEndpointWebExtension(configurationPropertiesReportEndpoint);
61+
}
62+
5263
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/documentation/ConfigurationPropertiesReportEndpointDocumentationTests.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,13 +36,31 @@
3636
* {@link ConfigurationPropertiesReportEndpoint}.
3737
*
3838
* @author Andy Wilkinson
39+
* @author Chris Bono
3940
*/
4041
class ConfigurationPropertiesReportEndpointDocumentationTests extends MockMvcEndpointDocumentationTests {
4142

4243
@Test
4344
void configProps() throws Exception {
4445
this.mockMvc.perform(get("/actuator/configprops")).andExpect(status().isOk())
45-
.andDo(MockMvcRestDocumentation.document("configprops",
46+
.andDo(MockMvcRestDocumentation.document("configprops/all",
47+
preprocessResponse(limit("contexts", getApplicationContext().getId(), "beans")),
48+
responseFields(fieldWithPath("contexts").description("Application contexts keyed by id."),
49+
fieldWithPath("contexts.*.beans.*")
50+
.description("`@ConfigurationProperties` beans keyed by bean name."),
51+
fieldWithPath("contexts.*.beans.*.prefix")
52+
.description("Prefix applied to the names of the bean's properties."),
53+
subsectionWithPath("contexts.*.beans.*.properties")
54+
.description("Properties of the bean as name-value pairs."),
55+
subsectionWithPath("contexts.*.beans.*.inputs").description(
56+
"Origin and value of the configuration property used when binding to this bean."),
57+
parentIdField())));
58+
}
59+
60+
@Test
61+
void configPropsFilterByPrefix() throws Exception {
62+
this.mockMvc.perform(get("/actuator/configprops/spring.resources")).andExpect(status().isOk())
63+
.andDo(MockMvcRestDocumentation.document("configprops/prefixed",
4664
preprocessResponse(limit("contexts", getApplicationContext().getId(), "beans")),
4765
responseFields(fieldWithPath("contexts").description("Application contexts keyed by id."),
4866
fieldWithPath("contexts.*.beans.*")

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/context/properties/ConfigurationPropertiesReportEndpoint.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.LinkedHashMap;
2727
import java.util.List;
2828
import java.util.Map;
29+
import java.util.function.Predicate;
2930
import java.util.stream.Collectors;
3031

3132
import com.fasterxml.jackson.annotation.JsonInclude.Include;
@@ -55,6 +56,7 @@
5556
import org.springframework.boot.actuate.endpoint.Sanitizer;
5657
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
5758
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
59+
import org.springframework.boot.actuate.endpoint.annotation.Selector;
5860
import org.springframework.boot.context.properties.BoundConfigurationProperties;
5961
import org.springframework.boot.context.properties.ConfigurationProperties;
6062
import org.springframework.boot.context.properties.ConfigurationPropertiesBean;
@@ -90,6 +92,7 @@
9092
* @author Stephane Nicoll
9193
* @author Madhura Bhave
9294
* @author Andy Wilkinson
95+
* @author Chris Bono
9396
* @since 2.0.0
9497
*/
9598
@Endpoint(id = "configprops")
@@ -114,15 +117,21 @@ public void setKeysToSanitize(String... keysToSanitize) {
114117

115118
@ReadOperation
116119
public ApplicationConfigurationProperties configurationProperties() {
117-
return extract(this.context);
120+
return extract(this.context, (bean) -> true);
118121
}
119122

120-
private ApplicationConfigurationProperties extract(ApplicationContext context) {
123+
@ReadOperation
124+
public ApplicationConfigurationProperties configurationPropertiesWithPrefix(@Selector String prefix) {
125+
return extract(this.context, (bean) -> bean.getAnnotation().prefix().startsWith(prefix));
126+
}
127+
128+
private ApplicationConfigurationProperties extract(ApplicationContext context,
129+
Predicate<ConfigurationPropertiesBean> beanFilterPredicate) {
121130
ObjectMapper mapper = getObjectMapper();
122131
Map<String, ContextConfigurationProperties> contexts = new HashMap<>();
123132
ApplicationContext target = context;
124133
while (target != null) {
125-
contexts.put(target.getId(), describeBeans(mapper, target));
134+
contexts.put(target.getId(), describeBeans(mapper, target, beanFilterPredicate));
126135
target = target.getParent();
127136
}
128137
return new ApplicationConfigurationProperties(contexts);
@@ -169,10 +178,12 @@ private void applySerializationModifier(ObjectMapper mapper) {
169178
mapper.setSerializerFactory(factory);
170179
}
171180

172-
private ContextConfigurationProperties describeBeans(ObjectMapper mapper, ApplicationContext context) {
181+
private ContextConfigurationProperties describeBeans(ObjectMapper mapper, ApplicationContext context,
182+
Predicate<ConfigurationPropertiesBean> beanFilterPredicate) {
173183
Map<String, ConfigurationPropertiesBean> beans = ConfigurationPropertiesBean.getAll(context);
174-
Map<String, ConfigurationPropertiesBeanDescriptor> descriptors = new HashMap<>();
175-
beans.forEach((beanName, bean) -> descriptors.put(beanName, describeBean(mapper, bean)));
184+
Map<String, ConfigurationPropertiesBeanDescriptor> descriptors = beans.values().stream()
185+
.filter(beanFilterPredicate)
186+
.collect(Collectors.toMap(ConfigurationPropertiesBean::getName, (bean) -> describeBean(mapper, bean)));
176187
return new ContextConfigurationProperties(descriptors,
177188
(context.getParent() != null) ? context.getParent().getId() : null);
178189
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.context.properties;
18+
19+
import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint.ApplicationConfigurationProperties;
20+
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
21+
import org.springframework.boot.actuate.endpoint.annotation.Selector;
22+
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
23+
import org.springframework.boot.actuate.endpoint.web.annotation.EndpointWebExtension;
24+
25+
/**
26+
* {@link EndpointWebExtension @EndpointWebExtension} for the
27+
* {@link ConfigurationPropertiesReportEndpoint}.
28+
*
29+
* @author Chris Bono
30+
* @since 2.4
31+
*/
32+
@EndpointWebExtension(endpoint = ConfigurationPropertiesReportEndpoint.class)
33+
public class ConfigurationPropertiesReportEndpointWebExtension {
34+
35+
private final ConfigurationPropertiesReportEndpoint delegate;
36+
37+
public ConfigurationPropertiesReportEndpointWebExtension(ConfigurationPropertiesReportEndpoint delegate) {
38+
this.delegate = delegate;
39+
}
40+
41+
@ReadOperation
42+
public WebEndpointResponse<ApplicationConfigurationProperties> configurationPropertiesWithPrefix(
43+
@Selector String prefix) {
44+
ApplicationConfigurationProperties configurationProperties = this.delegate
45+
.configurationPropertiesWithPrefix(prefix);
46+
boolean foundMatchingBeans = configurationProperties.getContexts().values().stream()
47+
.anyMatch((context) -> !context.getBeans().isEmpty());
48+
return (foundMatchingBeans) ? new WebEndpointResponse<>(configurationProperties, WebEndpointResponse.STATUS_OK)
49+
: new WebEndpointResponse<>(WebEndpointResponse.STATUS_NOT_FOUND);
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
/*
2+
* Copyright 2012-2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.context.properties;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint.ApplicationConfigurationProperties;
22+
import org.springframework.boot.actuate.context.properties.ConfigurationPropertiesReportEndpoint.ContextConfigurationProperties;
23+
import org.springframework.boot.context.properties.ConfigurationProperties;
24+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
25+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
import org.springframework.context.annotation.Bean;
27+
import org.springframework.context.annotation.Configuration;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link ConfigurationPropertiesReportEndpoint} when filtering by prefix.
33+
*
34+
* @author Chris Bono
35+
*/
36+
class ConfigurationPropertiesReportEndpointFilteringTests {
37+
38+
@Test
39+
void filterByPrefixSingleMatch() {
40+
ApplicationContextRunner contextRunner = new ApplicationContextRunner().withUserConfiguration(Config.class)
41+
.withPropertyValues("foo.primary.name:foo1", "foo.secondary.name:foo2", "only.bar.name:solo1");
42+
contextRunner.run((context) -> {
43+
ConfigurationPropertiesReportEndpoint endpoint = context
44+
.getBean(ConfigurationPropertiesReportEndpoint.class);
45+
ApplicationConfigurationProperties applicationProperties = endpoint
46+
.configurationPropertiesWithPrefix("only.bar");
47+
assertThat(applicationProperties.getContexts()).containsOnlyKeys(context.getId());
48+
ContextConfigurationProperties contextProperties = applicationProperties.getContexts().get(context.getId());
49+
assertThat(contextProperties.getBeans().values()).singleElement().hasFieldOrPropertyWithValue("prefix",
50+
"only.bar");
51+
});
52+
}
53+
54+
@Test
55+
void filterByPrefixMultipleMatches() {
56+
ApplicationContextRunner contextRunner = new ApplicationContextRunner().withUserConfiguration(Config.class)
57+
.withPropertyValues("foo.primary.name:foo1", "foo.secondary.name:foo2", "only.bar.name:solo1");
58+
contextRunner.run((context) -> {
59+
ConfigurationPropertiesReportEndpoint endpoint = context
60+
.getBean(ConfigurationPropertiesReportEndpoint.class);
61+
ApplicationConfigurationProperties applicationProperties = endpoint
62+
.configurationPropertiesWithPrefix("foo.");
63+
assertThat(applicationProperties.getContexts()).containsOnlyKeys(context.getId());
64+
ContextConfigurationProperties contextProperties = applicationProperties.getContexts().get(context.getId());
65+
assertThat(contextProperties.getBeans()).containsOnlyKeys("primaryFoo", "secondaryFoo");
66+
});
67+
}
68+
69+
@Test
70+
void filterByPrefixNoMatches() {
71+
ApplicationContextRunner contextRunner = new ApplicationContextRunner().withUserConfiguration(Config.class)
72+
.withPropertyValues("foo.primary.name:foo1", "foo.secondary.name:foo2", "only.bar.name:solo1");
73+
contextRunner.run((context) -> {
74+
ConfigurationPropertiesReportEndpoint endpoint = context
75+
.getBean(ConfigurationPropertiesReportEndpoint.class);
76+
ApplicationConfigurationProperties applicationProperties = endpoint
77+
.configurationPropertiesWithPrefix("foo.third");
78+
assertThat(applicationProperties.getContexts()).containsOnlyKeys(context.getId());
79+
ContextConfigurationProperties contextProperties = applicationProperties.getContexts().get(context.getId());
80+
assertThat(contextProperties.getBeans()).isEmpty();
81+
});
82+
}
83+
84+
@Configuration(proxyBeanMethods = false)
85+
@EnableConfigurationProperties(Bar.class)
86+
static class Config {
87+
88+
@Bean
89+
ConfigurationPropertiesReportEndpoint endpoint() {
90+
return new ConfigurationPropertiesReportEndpoint();
91+
}
92+
93+
@Bean
94+
@ConfigurationProperties(prefix = "foo.primary")
95+
Foo primaryFoo() {
96+
return new Foo();
97+
}
98+
99+
@Bean
100+
@ConfigurationProperties(prefix = "foo.secondary")
101+
Foo secondaryFoo() {
102+
return new Foo();
103+
}
104+
105+
}
106+
107+
public static class Foo {
108+
109+
private String name = "5150";
110+
111+
public String getName() {
112+
return this.name;
113+
}
114+
115+
public void setName(String name) {
116+
this.name = name;
117+
}
118+
119+
}
120+
121+
@ConfigurationProperties(prefix = "only.bar")
122+
public static class Bar {
123+
124+
private String name = "123456";
125+
126+
public String getName() {
127+
return this.name;
128+
}
129+
130+
public void setName(String name) {
131+
this.name = name;
132+
}
133+
134+
}
135+
136+
}

0 commit comments

Comments
 (0)