diff --git a/src/main/java/org/springframework/hateoas/config/HateoasConfiguration.java b/src/main/java/org/springframework/hateoas/config/HateoasConfiguration.java index 60a20025..d691770f 100644 --- a/src/main/java/org/springframework/hateoas/config/HateoasConfiguration.java +++ b/src/main/java/org/springframework/hateoas/config/HateoasConfiguration.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.Properties; import java.util.stream.Collectors; +import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.PropertiesFactoryBean; import org.springframework.context.ApplicationContext; @@ -48,6 +49,8 @@ import org.springframework.plugin.core.config.EnablePluginRegistries; import org.springframework.plugin.core.support.PluginRegistryFactoryBean; import org.springframework.util.ClassUtils; +import com.fasterxml.jackson.databind.ObjectMapper; + /** * Common HATEOAS specific configuration. * @@ -70,6 +73,12 @@ public class HateoasConfiguration { return MessageResolver.of(lookupMessageSource()); } + @Bean + WebConverters hypermediaWebMvcConverters(ObjectProvider mapper, + List information) { + return WebConverters.of(mapper.getIfUnique(ObjectMapper::new), information); + } + // RelProvider @Bean diff --git a/src/main/java/org/springframework/hateoas/config/RestTemplateHateoasConfiguration.java b/src/main/java/org/springframework/hateoas/config/RestTemplateHateoasConfiguration.java index 1fc87f7d..970278c1 100644 --- a/src/main/java/org/springframework/hateoas/config/RestTemplateHateoasConfiguration.java +++ b/src/main/java/org/springframework/hateoas/config/RestTemplateHateoasConfiguration.java @@ -35,7 +35,7 @@ class RestTemplateHateoasConfiguration { @Bean static HypermediaRestTemplateBeanPostProcessor hypermediaRestTemplateBeanPostProcessor( - WebMvcConverters converters) { + WebConverters converters) { return new HypermediaRestTemplateBeanPostProcessor(converters); } @@ -49,7 +49,7 @@ class RestTemplateHateoasConfiguration { @RequiredArgsConstructor static class HypermediaRestTemplateBeanPostProcessor implements BeanPostProcessor { - private final WebMvcConverters converters; + private final WebConverters converters; /* * (non-Javadoc) diff --git a/src/main/java/org/springframework/hateoas/config/WebMvcConverters.java b/src/main/java/org/springframework/hateoas/config/WebConverters.java similarity index 88% rename from src/main/java/org/springframework/hateoas/config/WebMvcConverters.java rename to src/main/java/org/springframework/hateoas/config/WebConverters.java index 88cca0ad..a9597a3c 100644 --- a/src/main/java/org/springframework/hateoas/config/WebMvcConverters.java +++ b/src/main/java/org/springframework/hateoas/config/WebConverters.java @@ -33,18 +33,18 @@ import com.fasterxml.jackson.databind.ObjectMapper; * * @author Oliver Drotbohm */ -class WebMvcConverters { +class WebConverters { private final List> converters; /** - * Creates a new {@link WebMvcConverters} from the given {@link ObjectMapper} and + * Creates a new {@link WebConverters} from the given {@link ObjectMapper} and * {@link HypermediaMappingInformation}s. * * @param mapper must not be {@literal null}. * @param mappingInformation must not be {@literal null}. */ - private WebMvcConverters(ObjectMapper mapper, List mappingInformation) { + private WebConverters(ObjectMapper mapper, List mappingInformation) { this.converters = mappingInformation.stream() // .map(it -> createMessageConverter(it, it.configureObjectMapper(mapper.copy()))) // @@ -52,19 +52,19 @@ class WebMvcConverters { } /** - * Creates a new {@link WebMvcConverters} from the given {@link ObjectMapper} and + * Creates a new {@link WebConverters} from the given {@link ObjectMapper} and * {@link HypermediaMappingInformation}s. * * @param mapper must not be {@literal null}. * @param mappingInformations must not be {@literal null}. * @return */ - public static WebMvcConverters of(ObjectMapper mapper, List mappingInformations) { + public static WebConverters of(ObjectMapper mapper, List mappingInformations) { Assert.notNull(mapper, "ObjectMapper must not be null!"); Assert.notNull(mappingInformations, "Mapping information must not be null!"); - return new WebMvcConverters(mapper, mappingInformations); + return new WebConverters(mapper, mappingInformations); } /** diff --git a/src/main/java/org/springframework/hateoas/config/WebFluxHateoasConfiguration.java b/src/main/java/org/springframework/hateoas/config/WebFluxHateoasConfiguration.java index ab30379b..df72025f 100644 --- a/src/main/java/org/springframework/hateoas/config/WebFluxHateoasConfiguration.java +++ b/src/main/java/org/springframework/hateoas/config/WebFluxHateoasConfiguration.java @@ -47,12 +47,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; @Configuration class WebFluxHateoasConfiguration { - @Bean - WebMvcConverters hypermediaWebMvcConverters(ObjectProvider mapper, - List information) { - return WebMvcConverters.of(mapper.getIfUnique(ObjectMapper::new), information); - } - @Bean WebFluxCodecs hypermediaConverters(ObjectProvider mapper, List mappingInformation) { diff --git a/src/main/java/org/springframework/hateoas/config/WebMvcHateoasConfiguration.java b/src/main/java/org/springframework/hateoas/config/WebMvcHateoasConfiguration.java index c5f163e7..4c57e796 100644 --- a/src/main/java/org/springframework/hateoas/config/WebMvcHateoasConfiguration.java +++ b/src/main/java/org/springframework/hateoas/config/WebMvcHateoasConfiguration.java @@ -38,8 +38,6 @@ import org.springframework.web.method.support.HandlerMethodReturnValueHandlerCom import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; -import com.fasterxml.jackson.databind.ObjectMapper; - /** * Spring MVC HATEOAS Configuration * @@ -51,13 +49,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; class WebMvcHateoasConfiguration { @Bean - WebMvcConverters hypermediaWebMvcConverters(ObjectProvider mapper, - List information) { - return WebMvcConverters.of(mapper.getIfUnique(ObjectMapper::new), information); - } - - @Bean - HypermediaWebMvcConfigurer hypermediaWebMvcConfigurer(WebMvcConverters converters) { + HypermediaWebMvcConfigurer hypermediaWebMvcConfigurer(WebConverters converters) { return new HypermediaWebMvcConfigurer(converters); } @@ -90,7 +82,7 @@ class WebMvcHateoasConfiguration { @RequiredArgsConstructor static class HypermediaWebMvcConfigurer implements WebMvcConfigurer { - private final @NonNull WebMvcConverters hypermediaConverters; + private final @NonNull WebConverters hypermediaConverters; /* * (non-Javadoc) diff --git a/src/test/java/org/springframework/hateoas/config/RestTemplateHateoasConfigurationIntegrationTest.java b/src/test/java/org/springframework/hateoas/config/RestTemplateHateoasConfigurationIntegrationTest.java new file mode 100644 index 00000000..a9047e51 --- /dev/null +++ b/src/test/java/org/springframework/hateoas/config/RestTemplateHateoasConfigurationIntegrationTest.java @@ -0,0 +1,48 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.hateoas.config; + +import org.junit.jupiter.api.Test; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Import; +import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType; +import org.springframework.hateoas.support.ContextTester; + +/** + * Integration tests for {@link RestTemplateHateoasConfiguration}. + * + * @author Oliver Drotbohm + */ +class RestTemplateHateoasConfigurationIntegrationTest { + + @Test // #1119 + void bootstrapsWithManualImports() { + ContextTester.withContext(WithImports.class, context -> {}); + } + + @Test // #1119 + void bootstrapsViaAnnotationConfiguration() { + ContextTester.withContext(AnnotationConfig.class, context -> {}); + } + + @Configuration + @Import({ HateoasConfiguration.class, RestTemplateHateoasConfiguration.class }) + static class WithImports {} + + @Configuration + @EnableHypermediaSupport(type = HypermediaType.HAL) + static class AnnotationConfig {} +}