Skip to content

Commit bb2864a

Browse files
michael-simonswilkinsona
authored andcommitted
Auto-configure a ResourceConfig for Jersey endpoints if needed
See gh-11948
1 parent d5e4a19 commit bb2864a

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-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,77 @@
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+
* @author Michael Simons
35+
*/
36+
public class JerseyWebEndpointManagementContextConfigurationTests {
37+
38+
private final WebApplicationContextRunner runner = new WebApplicationContextRunner()
39+
.withConfiguration(AutoConfigurations.of(WebEndpointAutoConfiguration.class, JerseyWebEndpointManagementContextConfiguration.class));
40+
41+
@Test
42+
public void contextShouldContainSingleResourceConfig() {
43+
this.runner
44+
.withUserConfiguration(WebEndpointsSupplierConfig.class)
45+
.run(context -> assertThat(context).hasSingleBean(ResourceConfig.class));
46+
}
47+
48+
@Test
49+
public void contextWhenResourceConfigExistsShouldContainSingleResourceConfig() {
50+
this.runner
51+
.withUserConfiguration(
52+
WebEndpointsSupplierConfig.class,
53+
ConfigWithResourceConfig.class)
54+
.run(context -> {
55+
assertThat(context).hasSingleBean(ResourceConfig.class);
56+
assertThat(context).hasBean("customResourceConfig");
57+
});
58+
}
59+
60+
@Configuration
61+
static class WebEndpointsSupplierConfig {
62+
63+
@Bean
64+
public WebEndpointsSupplier webEndpointsSupplier() {
65+
return () -> Collections.emptyList();
66+
}
67+
}
68+
69+
@Configuration
70+
static class ConfigWithResourceConfig {
71+
72+
@Bean
73+
public ResourceConfig customResourceConfig() {
74+
return new ResourceConfig();
75+
}
76+
}
77+
}

0 commit comments

Comments
 (0)