From a27176807f4b45bc768e019d9a0dab1ac8ec3dec Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 8 Feb 2016 15:34:22 +0100 Subject: [PATCH] Polish contribution Closes gh-4188 --- .../jersey/JerseyAutoConfiguration.java | 25 ++++++++++++------- .../jersey/ResourceConfigCustomizer.java | 10 +++++--- ...rationCustomObjectMapperProviderTests.java | 7 +++--- ...onfigurationObjectMapperProviderTests.java | 6 ++--- .../main/asciidoc/spring-boot-features.adoc | 3 +++ 5 files changed, 32 insertions(+), 19 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java index 163b669146..252ea2c2e8 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.java @@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.jersey; import java.util.Arrays; import java.util.EnumSet; +import java.util.List; import java.util.Map.Entry; import javax.annotation.PostConstruct; @@ -48,6 +49,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; @@ -58,6 +60,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; +import org.springframework.core.annotation.AnnotationAwareOrderComparator; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.Order; import org.springframework.util.ClassUtils; @@ -93,20 +96,14 @@ public class JerseyAutoConfiguration implements ServletContextAware { private ResourceConfig config; @Autowired(required = false) - private ResourceConfigCustomizer customizer; + private List customizers; private String path; @PostConstruct public void path() { resolveApplicationPath(); - applyCustomConfig(); - } - - private void applyCustomConfig() { - if (this.customizer != null) { - this.customizer.customize(this.config); - } + customize(); } private void resolveApplicationPath() { @@ -119,6 +116,15 @@ public class JerseyAutoConfiguration implements ServletContextAware { } } + private void customize() { + if (this.customizers != null) { + AnnotationAwareOrderComparator.sort(this.customizers); + for (ResourceConfigCustomizer customizer : this.customizers) { + customizer.customize(this.config); + } + } + } + @Bean @ConditionalOnMissingBean public FilterRegistrationBean requestContextFilter() { @@ -218,8 +224,9 @@ public class JerseyAutoConfiguration implements ServletContextAware { } @ConditionalOnClass(JacksonFeature.class) + @ConditionalOnSingleCandidate(ObjectMapper.class) @Configuration - static class ObjectMapperResourceConfigCustomizer { + static class JacksonResourceConfigCustomizer { @Bean public ResourceConfigCustomizer resourceConfigCustomizer() { diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/ResourceConfigCustomizer.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/ResourceConfigCustomizer.java index 030d8912d8..0bb08cc030 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/ResourceConfigCustomizer.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jersey/ResourceConfigCustomizer.java @@ -18,17 +18,19 @@ package org.springframework.boot.autoconfigure.jersey; import org.glassfish.jersey.server.ResourceConfig; -import org.springframework.context.annotation.Configuration; - /** - * Callback for customizing the Jersey {@link Configuration}. + * Callback interface that can be implemented by beans wishing to customize Jersey's + * {@link ResourceConfig} before it is used. * * @author Eddú Meléndez * @since 1.4.0 - * @see JerseyAutoConfiguration */ public interface ResourceConfigCustomizer { + /** + * Customize the resource config. + * @param config the {@link ResourceConfig} to customize + */ void customize(ResourceConfig config); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomObjectMapperProviderTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomObjectMapperProviderTests.java index b581f589f5..ec2b55812a 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomObjectMapperProviderTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomObjectMapperProviderTests.java @@ -47,7 +47,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.web.client.RestTemplate; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; + /** * @author Eddú Meléndez @@ -67,8 +68,8 @@ public class JerseyAutoConfigurationCustomObjectMapperProviderTests { public void contextLoads() { ResponseEntity response = this.restTemplate.getForEntity( "http://localhost:" + this.port + "/rest/message", String.class); - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals("{\"subject\":\"Jersey\"}", response.getBody()); + assertThat(HttpStatus.OK).isEqualTo(response.getStatusCode()); + assertThat("{\"subject\":\"Jersey\"}").isEqualTo(response.getBody()); } @MinimalWebConfiguration diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationObjectMapperProviderTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationObjectMapperProviderTests.java index 44c49fe378..87234ee25a 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationObjectMapperProviderTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationObjectMapperProviderTests.java @@ -47,7 +47,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.web.client.RestTemplate; -import static org.junit.Assert.assertEquals; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Eddú Meléndez @@ -67,8 +67,8 @@ public class JerseyAutoConfigurationObjectMapperProviderTests { public void contextLoads() { ResponseEntity response = this.restTemplate.getForEntity( "http://localhost:" + this.port + "/rest/message", String.class); - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals("{\"subject\":\"Jersey\",\"body\":null}", response.getBody()); + assertThat(HttpStatus.OK).isEqualTo(response.getStatusCode()); + assertThat("{\"subject\":\"Jersey\",\"body\":null}").isEqualTo(response.getBody()); } @MinimalWebConfiguration diff --git a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc index ab4dac8ff7..57c77b3bc8 100644 --- a/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc +++ b/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc @@ -1754,6 +1754,9 @@ all the endpoints: } ---- +You can also register an arbitrary number of beans implementing `ResourceConfigCustomizer` +for more advanced customizations. + All the registered endpoints should be `@Components` with HTTP resource annotations (`@GET` etc.), e.g.