DATAREST-1256 - Upgraded to Spring HATEOAS 0.25.0.BUILD-SNAPSHOT.

Adapted RepositoryRestMvcConfiguration to now create a shared fallback ObjectMapper and avoid to register it as Spring Bean. Adapted test cases that previously were relying on such a bean being present.

Adapted test cases to verify on the correct ALPS document structure (see spring-projects/spring-hateoas#665).
This commit is contained in:
Oliver Gierke
2018-06-20 20:01:40 +02:00
parent 06f775abd0
commit 954416a2b9
8 changed files with 89 additions and 49 deletions

View File

@@ -22,6 +22,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import org.springframework.beans.factory.BeanClassLoaderAware;
@@ -29,6 +30,7 @@ import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.PropertiesFactoryBean;
@@ -117,6 +119,7 @@ import org.springframework.data.rest.webmvc.support.JpaHelper;
import org.springframework.data.rest.webmvc.support.PagingAndSortingTemplateVariables;
import org.springframework.data.rest.webmvc.support.RepositoryEntityLinks;
import org.springframework.data.util.AnnotatedTypeScanner;
import org.springframework.data.util.Lazy;
import org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver;
import org.springframework.data.web.HateoasSortHandlerMethodArgumentResolver;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
@@ -132,6 +135,7 @@ import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
import org.springframework.hateoas.core.EvoInflectorRelProvider;
import org.springframework.hateoas.hal.CurieProvider;
import org.springframework.hateoas.hal.HalConfiguration;
import org.springframework.hateoas.hal.Jackson2HalModule;
import org.springframework.hateoas.hal.Jackson2HalModule.HalHandlerInstantiator;
import org.springframework.hateoas.mvc.ResourceProcessorInvoker;
@@ -189,15 +193,32 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
@Autowired(required = false) List<RepositoryRestConfigurer> configurers = Collections.emptyList();
@Autowired(required = false) List<EntityLookup<?>> lookups = Collections.emptyList();
@Autowired(required = false) RelProvider relProvider;
@Autowired(required = false) CurieProvider curieProvider;
@Autowired Optional<RelProvider> relProvider;
@Autowired Optional<CurieProvider> curieProvider;
@Autowired Optional<HalConfiguration> halConfiguration;
@Autowired ObjectProvider<ObjectMapper> objectMapper;
private final Lazy<ObjectMapper> mapper;
private RepositoryRestConfigurerDelegate configurerDelegate;
private ClassLoader beanClassLoader;
public RepositoryRestMvcConfiguration(ApplicationContext context,
@Qualifier("mvcConversionService") ObjectFactory<ConversionService> conversionService) {
super(context, conversionService);
this.mapper = Lazy.of(() -> {
Jdk8Module jdk8Module = new Jdk8Module();
jdk8Module.configureAbsentsAsNulls(true);
ObjectMapper mapper = basicObjectMapper();
mapper.registerModule(persistentEntityJackson2Module());
mapper.registerModule(jdk8Module);
return mapper;
});
}
/*
@@ -461,17 +482,8 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
*
* @return
*/
@Bean
public ObjectMapper objectMapper() {
Jdk8Module jdk8Module = new Jdk8Module();
jdk8Module.configureAbsentsAsNulls(true);
ObjectMapper mapper = basicObjectMapper();
mapper.registerModule(persistentEntityJackson2Module());
mapper.registerModule(jdk8Module);
return mapper;
return mapper.get();
}
/**
@@ -529,13 +541,13 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
return converter;
}
@Bean
public ObjectMapper halObjectMapper() {
RelProvider defaultedRelProvider = this.relProvider != null ? this.relProvider : new EvoInflectorRelProvider();
RelProvider defaultedRelProvider = this.relProvider.orElseGet(() -> new EvoInflectorRelProvider());
HalConfiguration halConfiguration = this.halConfiguration.orElseGet(() -> new HalConfiguration());
HalHandlerInstantiator instantiator = new HalHandlerInstantiator(defaultedRelProvider, curieProvider,
resourceDescriptionMessageSourceAccessor(), applicationContext.getAutowireCapableBeanFactory());
HalHandlerInstantiator instantiator = new HalHandlerInstantiator(defaultedRelProvider, curieProvider.orElse(null),
resourceDescriptionMessageSourceAccessor(), halConfiguration);
ObjectMapper mapper = basicObjectMapper();
mapper.registerModule(persistentEntityJackson2Module());
@@ -849,7 +861,8 @@ public class RepositoryRestMvcConfiguration extends HateoasAwareSpringDataWebCon
protected ObjectMapper basicObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
ObjectMapper mapper = this.objectMapper.getIfAvailable();
ObjectMapper objectMapper = mapper == null ? new ObjectMapper() : mapper.copy();
objectMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

View File

@@ -17,14 +17,15 @@ package org.springframework.data.rest.webmvc.config;
import static org.assertj.core.api.Assertions.*;
import javax.naming.Name;
import javax.naming.ldap.LdapName;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import javax.naming.Name;
import javax.naming.ldap.LdapName;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
@@ -54,6 +55,7 @@ import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.core.DefaultRelProvider;
import org.springframework.hateoas.mvc.TypeConstrainedMappingJackson2HttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.util.MultiValueMap;
@@ -97,9 +99,7 @@ public class RepositoryRestMvConfigurationIntegrationTests {
context.getBean(PageableHandlerMethodArgumentResolver.class);
// Verify HAL setup
context.getBean("halJacksonHttpMessageConverter", HttpMessageConverter.class);
ObjectMapper mapper = context.getBean("halObjectMapper", ObjectMapper.class);
mapper.writeValueAsString(new RepositoryLinksResource());
getObjectMapper().writeValueAsString(new RepositoryLinksResource());
}
@Test // DATAREST-271
@@ -126,7 +126,7 @@ public class RepositoryRestMvConfigurationIntegrationTests {
Sample sample = new Sample();
sample.date = new Date();
ObjectMapper mapper = context.getBean("objectMapper", ObjectMapper.class);
ObjectMapper mapper = getObjectMapper();
DateFormatter formatter = new DateFormatter();
formatter.setPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
@@ -207,6 +207,13 @@ public class RepositoryRestMvConfigurationIntegrationTests {
assertThat((String) ReflectionTestUtils.getField(messageSource, "defaultEncoding")).isEqualTo("UTF-8");
}
private static ObjectMapper getObjectMapper() {
AbstractJackson2HttpMessageConverter converter = context.getBean("halJacksonHttpMessageConverter",
AbstractJackson2HttpMessageConverter.class);
return converter.getObjectMapper();
}
@Configuration
@Import(RepositoryRestMvcConfiguration.class)
static class ExtendingConfiguration extends RepositoryRestConfigurerAdapter {