#1128 - Moved WebConverters bean declaration into common HateoasConfiguration.

RestTemplateHateoasConfiguration depends on a shared bean WebConverters (previously WebMvcConverters). As it can safely be used in both WebFlux and WebMVC scenarios (as it only depends on types from spring-web), we now include a declaration in the shared HateoasConfiguration. This also allows us to remove the just introduced explicit declaration of a WebConverter bean in WebFluxHateoasConfiguration.
This commit is contained in:
Oliver Drotbohm
2019-12-01 22:52:54 +01:00
parent 20238e26b6
commit ceb80542ac
6 changed files with 67 additions and 24 deletions

View File

@@ -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<ObjectMapper> mapper,
List<HypermediaMappingInformation> information) {
return WebConverters.of(mapper.getIfUnique(ObjectMapper::new), information);
}
// RelProvider
@Bean

View File

@@ -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)

View File

@@ -33,18 +33,18 @@ import com.fasterxml.jackson.databind.ObjectMapper;
*
* @author Oliver Drotbohm
*/
class WebMvcConverters {
class WebConverters {
private final List<HttpMessageConverter<?>> 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<HypermediaMappingInformation> mappingInformation) {
private WebConverters(ObjectMapper mapper, List<HypermediaMappingInformation> 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<HypermediaMappingInformation> mappingInformations) {
public static WebConverters of(ObjectMapper mapper, List<HypermediaMappingInformation> 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);
}
/**

View File

@@ -47,12 +47,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@Configuration
class WebFluxHateoasConfiguration {
@Bean
WebMvcConverters hypermediaWebMvcConverters(ObjectProvider<ObjectMapper> mapper,
List<HypermediaMappingInformation> information) {
return WebMvcConverters.of(mapper.getIfUnique(ObjectMapper::new), information);
}
@Bean
WebFluxCodecs hypermediaConverters(ObjectProvider<ObjectMapper> mapper,
List<HypermediaMappingInformation> mappingInformation) {

View File

@@ -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<ObjectMapper> mapper,
List<HypermediaMappingInformation> 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)

View File

@@ -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 {}
}