Polish contribution

Closes gh-4188
This commit is contained in:
Stephane Nicoll 2016-02-08 15:34:22 +01:00
parent 5776d6a8d7
commit a27176807f
5 changed files with 32 additions and 19 deletions

View File

@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.jersey;
import java.util.Arrays; import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import javax.annotation.PostConstruct; 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.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 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.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration; import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.DispatcherServletAutoConfiguration; 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.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.annotation.AnnotationUtils; import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order; import org.springframework.core.annotation.Order;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
@ -93,20 +96,14 @@ public class JerseyAutoConfiguration implements ServletContextAware {
private ResourceConfig config; private ResourceConfig config;
@Autowired(required = false) @Autowired(required = false)
private ResourceConfigCustomizer customizer; private List<ResourceConfigCustomizer> customizers;
private String path; private String path;
@PostConstruct @PostConstruct
public void path() { public void path() {
resolveApplicationPath(); resolveApplicationPath();
applyCustomConfig(); customize();
}
private void applyCustomConfig() {
if (this.customizer != null) {
this.customizer.customize(this.config);
}
} }
private void resolveApplicationPath() { 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 @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public FilterRegistrationBean requestContextFilter() { public FilterRegistrationBean requestContextFilter() {
@ -218,8 +224,9 @@ public class JerseyAutoConfiguration implements ServletContextAware {
} }
@ConditionalOnClass(JacksonFeature.class) @ConditionalOnClass(JacksonFeature.class)
@ConditionalOnSingleCandidate(ObjectMapper.class)
@Configuration @Configuration
static class ObjectMapperResourceConfigCustomizer { static class JacksonResourceConfigCustomizer {
@Bean @Bean
public ResourceConfigCustomizer resourceConfigCustomizer() { public ResourceConfigCustomizer resourceConfigCustomizer() {

View File

@ -18,17 +18,19 @@ package org.springframework.boot.autoconfigure.jersey;
import org.glassfish.jersey.server.ResourceConfig; 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 * @author Eddú Meléndez
* @since 1.4.0 * @since 1.4.0
* @see JerseyAutoConfiguration
*/ */
public interface ResourceConfigCustomizer { public interface ResourceConfigCustomizer {
/**
* Customize the resource config.
* @param config the {@link ResourceConfig} to customize
*/
void customize(ResourceConfig config); void customize(ResourceConfig config);
} }

View File

@ -47,7 +47,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertEquals; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* @author Eddú Meléndez * @author Eddú Meléndez
@ -67,8 +68,8 @@ public class JerseyAutoConfigurationCustomObjectMapperProviderTests {
public void contextLoads() { public void contextLoads() {
ResponseEntity<String> response = this.restTemplate.getForEntity( ResponseEntity<String> response = this.restTemplate.getForEntity(
"http://localhost:" + this.port + "/rest/message", String.class); "http://localhost:" + this.port + "/rest/message", String.class);
assertEquals(HttpStatus.OK, response.getStatusCode()); assertThat(HttpStatus.OK).isEqualTo(response.getStatusCode());
assertEquals("{\"subject\":\"Jersey\"}", response.getBody()); assertThat("{\"subject\":\"Jersey\"}").isEqualTo(response.getBody());
} }
@MinimalWebConfiguration @MinimalWebConfiguration

View File

@ -47,7 +47,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import static org.junit.Assert.assertEquals; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* @author Eddú Meléndez * @author Eddú Meléndez
@ -67,8 +67,8 @@ public class JerseyAutoConfigurationObjectMapperProviderTests {
public void contextLoads() { public void contextLoads() {
ResponseEntity<String> response = this.restTemplate.getForEntity( ResponseEntity<String> response = this.restTemplate.getForEntity(
"http://localhost:" + this.port + "/rest/message", String.class); "http://localhost:" + this.port + "/rest/message", String.class);
assertEquals(HttpStatus.OK, response.getStatusCode()); assertThat(HttpStatus.OK).isEqualTo(response.getStatusCode());
assertEquals("{\"subject\":\"Jersey\",\"body\":null}", response.getBody()); assertThat("{\"subject\":\"Jersey\",\"body\":null}").isEqualTo(response.getBody());
} }
@MinimalWebConfiguration @MinimalWebConfiguration

View File

@ -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 All the registered endpoints should be `@Components` with HTTP resource annotations
(`@GET` etc.), e.g. (`@GET` etc.), e.g.