Skip to content

Commit 64a62db

Browse files
committed
Merge pull request #11948 from Michael Simons
* gh-11948: Polish "Auto-configure a ResourceConfig for Jersey endpoints if needed" Auto-configure a ResourceConfig for Jersey endpoints if needed
2 parents d5e4a19 + a325b13 commit 64a62db

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/jersey/JerseyWebEndpointManagementContextConfiguration.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,21 @@
4949
*
5050
* @author Andy Wilkinson
5151
* @author Phillip Webb
52+
* @author Michael Simons
5253
*/
5354
@Configuration
5455
@ConditionalOnWebApplication(type = Type.SERVLET)
5556
@ConditionalOnClass(ResourceConfig.class)
56-
@ConditionalOnBean({ ResourceConfig.class, WebEndpointsSupplier.class })
57+
@ConditionalOnBean(WebEndpointsSupplier.class)
5758
@ConditionalOnMissingBean(type = "org.springframework.web.servlet.DispatcherServlet")
5859
class JerseyWebEndpointManagementContextConfiguration {
5960

61+
@ConditionalOnMissingBean(ResourceConfig.class)
62+
@Bean
63+
public ResourceConfig resourceConfig() {
64+
return new ResourceConfig();
65+
}
66+
6067
@Bean
6168
public ResourceConfigCustomizer webEndpointRegistrar(
6269
WebEndpointsSupplier webEndpointsSupplier,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2012-2018 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+
* http://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.autoconfigure.endpoint.web.jersey;
18+
19+
import java.util.Collections;
20+
21+
import org.glassfish.jersey.server.ResourceConfig;
22+
import org.junit.Test;
23+
24+
import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration;
25+
import org.springframework.boot.actuate.endpoint.web.WebEndpointsSupplier;
26+
import org.springframework.boot.autoconfigure.AutoConfigurations;
27+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
/**
34+
* Tests for {@link JerseyWebEndpointManagementContextConfiguration}.
35+
*
36+
* @author Michael Simons
37+
*/
38+
public class JerseyWebEndpointManagementContextConfigurationTests {
39+
40+
private final WebApplicationContextRunner runner = new WebApplicationContextRunner()
41+
.withConfiguration(AutoConfigurations.of(WebEndpointAutoConfiguration.class,
42+
JerseyWebEndpointManagementContextConfiguration.class));
43+
44+
@Test
45+
public void resourceConfigIsAutoConfiguredWhenNeeded() {
46+
this.runner.withUserConfiguration(WebEndpointsSupplierConfig.class).run(
47+
(context) -> assertThat(context).hasSingleBean(ResourceConfig.class));
48+
}
49+
50+
@Test
51+
public void existingResourceConfigIsUsedWhenAvailable() {
52+
this.runner.withUserConfiguration(WebEndpointsSupplierConfig.class,
53+
ConfigWithResourceConfig.class).run((context) -> {
54+
assertThat(context).hasSingleBean(ResourceConfig.class);
55+
assertThat(context).hasBean("customResourceConfig");
56+
});
57+
}
58+
59+
@Configuration
60+
static class WebEndpointsSupplierConfig {
61+
62+
@Bean
63+
public WebEndpointsSupplier webEndpointsSupplier() {
64+
return () -> Collections.emptyList();
65+
}
66+
67+
}
68+
69+
@Configuration
70+
static class ConfigWithResourceConfig {
71+
72+
@Bean
73+
public ResourceConfig customResourceConfig() {
74+
return new ResourceConfig();
75+
}
76+
77+
}
78+
79+
}

0 commit comments

Comments
 (0)