Skip to content

Commit 3e3353e

Browse files
committed
Merge branch '2.1.x'
2 parents bece962 + f417fa5 commit 3e3353e

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

Diff for: spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java

+8-13
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.servlet.ServletException;
2626
import javax.servlet.ServletRegistration;
2727
import javax.ws.rs.ext.ContextResolver;
28+
import javax.xml.bind.annotation.XmlElement;
2829

2930
import com.fasterxml.jackson.databind.AnnotationIntrospector;
3031
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -39,6 +40,7 @@
3940
import org.glassfish.jersey.servlet.ServletProperties;
4041

4142
import org.springframework.beans.factory.ObjectProvider;
43+
import org.springframework.beans.factory.annotation.Autowired;
4244
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
4345
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
4446
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
@@ -201,34 +203,27 @@ public void onStartup(ServletContext servletContext) throws ServletException {
201203

202204
}
203205

206+
@Configuration(proxyBeanMethods = false)
204207
@ConditionalOnClass(JacksonFeature.class)
205208
@ConditionalOnSingleCandidate(ObjectMapper.class)
206-
@Configuration(proxyBeanMethods = false)
207209
static class JacksonResourceConfigCustomizer {
208210

209-
private static final String JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME = "com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector";
210-
211211
@Bean
212212
public ResourceConfigCustomizer resourceConfigCustomizer(
213213
final ObjectMapper objectMapper) {
214-
addJaxbAnnotationIntrospectorIfPresent(objectMapper);
215214
return (ResourceConfig config) -> {
216215
config.register(JacksonFeature.class);
217216
config.register(new ObjectMapperContextResolver(objectMapper),
218217
ContextResolver.class);
219218
};
220219
}
221220

222-
private void addJaxbAnnotationIntrospectorIfPresent(ObjectMapper objectMapper) {
223-
if (ClassUtils.isPresent(JAXB_ANNOTATION_INTROSPECTOR_CLASS_NAME,
224-
getClass().getClassLoader())) {
225-
new ObjectMapperCustomizer().addJaxbAnnotationIntrospector(objectMapper);
226-
}
227-
}
228-
229-
private static final class ObjectMapperCustomizer {
221+
@Configuration
222+
@ConditionalOnClass({ JaxbAnnotationIntrospector.class, XmlElement.class })
223+
static class JaxbObjectMapperCustomizer {
230224

231-
private void addJaxbAnnotationIntrospector(ObjectMapper objectMapper) {
225+
@Autowired
226+
public void addJaxbAnnotationIntrospector(ObjectMapper objectMapper) {
232227
JaxbAnnotationIntrospector jaxbAnnotationIntrospector = new JaxbAnnotationIntrospector(
233228
objectMapper.getTypeFactory());
234229
objectMapper.setAnnotationIntrospectors(

Diff for: spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationTests.java

+46
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@
1616

1717
package org.springframework.boot.autoconfigure.jersey;
1818

19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector;
1921
import org.glassfish.jersey.server.ResourceConfig;
2022
import org.junit.Test;
2123

2224
import org.springframework.boot.autoconfigure.AutoConfigurations;
25+
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
2326
import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener;
2427
import org.springframework.boot.logging.LogLevel;
28+
import org.springframework.boot.test.context.FilteredClassLoader;
2529
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
2630
import org.springframework.boot.web.servlet.FilterRegistrationBean;
2731
import org.springframework.context.annotation.Bean;
@@ -72,6 +76,48 @@ public void whenUserDefinesARequestContextFilterRegistrationTheAutoConfiguredReg
7276
});
7377
}
7478

79+
@Test
80+
public void whenJaxbIsAvailableTheObjectMapperIsCustomizedWithAnAnnotationIntrospector() {
81+
this.contextRunner
82+
.withConfiguration(AutoConfigurations.of(JacksonAutoConfiguration.class))
83+
.run((context) -> {
84+
ObjectMapper objectMapper = context.getBean(ObjectMapper.class);
85+
assertThat(objectMapper.getSerializationConfig()
86+
.getAnnotationIntrospector().allIntrospectors().stream()
87+
.filter(JaxbAnnotationIntrospector.class::isInstance))
88+
.hasSize(1);
89+
});
90+
}
91+
92+
@Test
93+
public void whenJaxbIsNotAvailableTheObjectMapperCustomizationBacksOff() {
94+
this.contextRunner
95+
.withConfiguration(AutoConfigurations.of(JacksonAutoConfiguration.class))
96+
.withClassLoader(new FilteredClassLoader("javax.xml.bind.annotation"))
97+
.run((context) -> {
98+
ObjectMapper objectMapper = context.getBean(ObjectMapper.class);
99+
assertThat(objectMapper.getSerializationConfig()
100+
.getAnnotationIntrospector().allIntrospectors().stream()
101+
.filter(JaxbAnnotationIntrospector.class::isInstance))
102+
.isEmpty();
103+
});
104+
}
105+
106+
@Test
107+
public void whenJacksonJaxbModuleIsNotAvailableTheObjectMapperCustomizationBacksOff() {
108+
this.contextRunner
109+
.withConfiguration(AutoConfigurations.of(JacksonAutoConfiguration.class))
110+
.withClassLoader(
111+
new FilteredClassLoader(JaxbAnnotationIntrospector.class))
112+
.run((context) -> {
113+
ObjectMapper objectMapper = context.getBean(ObjectMapper.class);
114+
assertThat(objectMapper.getSerializationConfig()
115+
.getAnnotationIntrospector().allIntrospectors().stream()
116+
.filter(JaxbAnnotationIntrospector.class::isInstance))
117+
.isEmpty();
118+
});
119+
}
120+
75121
@Configuration(proxyBeanMethods = false)
76122
static class ResourceConfigConfiguration {
77123

Diff for: spring-boot-project/spring-boot-starters/spring-boot-starter-jersey/pom.xml

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@
113113
<groupId>org.glassfish.jersey.media</groupId>
114114
<artifactId>jersey-media-json-jackson</artifactId>
115115
</dependency>
116+
<dependency>
117+
<groupId>javax.xml.bind</groupId>
118+
<artifactId>jaxb-api</artifactId>
119+
</dependency>
116120
</dependencies>
117121
<build>
118122
<plugins>

0 commit comments

Comments
 (0)