parent
5776d6a8d7
commit
a27176807f
|
@ -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<ResourceConfigCustomizer> 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() {
|
||||
|
|
|
@ -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);
|
||||
|
||||
}
|
||||
|
|
|
@ -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<String> 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
|
||||
|
|
|
@ -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<String> 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
|
||||
|
|
|
@ -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.
|
||||
|
||||
|
|
Loading…
Reference in New Issue