Commit f43d6925 authored by Andy Wilkinson's avatar Andy Wilkinson

Apply spring.jackson.* config to HypermediaAutoConfiguration’s ObjectMapper

Previously, HypermediaAutoConfiguration would trigger the creation of
an ObjectMapper bean named _halObjectMapper. This bean did not have the
spring.jackson.* configuration applied to it, however its presence
would revent JacksonAutoConfiguration from creating its
ObjectMapper. This left the user with an ObjectMapper that did not
honour the spring.jackson.* configuration.

This commit updates HypermediaAutoConfiguration to use the
Jackson2ObjectMapperBuilder that may have been created by
JacksonAutoConfiguration. If the builder exists it is used to configure
the _halObjectMapper bean.

Fixes gh-1949
parent 35b7ba5c
...@@ -16,12 +16,17 @@ ...@@ -16,12 +16,17 @@
package org.springframework.boot.autoconfigure.hateoas; package org.springframework.boot.autoconfigure.hateoas;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
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.ConditionalOnWebApplication; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.hateoas.EntityLinks; import org.springframework.hateoas.EntityLinks;
import org.springframework.hateoas.LinkDiscoverers; import org.springframework.hateoas.LinkDiscoverers;
...@@ -29,27 +34,63 @@ import org.springframework.hateoas.Resource; ...@@ -29,27 +34,63 @@ import org.springframework.hateoas.Resource;
import org.springframework.hateoas.config.EnableEntityLinks; import org.springframework.hateoas.config.EnableEntityLinks;
import org.springframework.hateoas.config.EnableHypermediaSupport; import org.springframework.hateoas.config.EnableHypermediaSupport;
import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType; import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.plugin.core.Plugin; import org.springframework.plugin.core.Plugin;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import com.fasterxml.jackson.databind.ObjectMapper;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for Spring HATEOAS's * {@link EnableAutoConfiguration Auto-configuration} for Spring HATEOAS's
* {@link EnableHypermediaSupport}. * {@link EnableHypermediaSupport}.
* *
* @author Roy Clarkson * @author Roy Clarkson
* @author Oliver Gierke * @author Oliver Gierke
* @author Andy Wilkinson
* @since 1.1.0 * @since 1.1.0
*/ */
@Configuration @Configuration
@ConditionalOnClass({ Resource.class, RequestMapping.class, Plugin.class }) @ConditionalOnClass({ Resource.class, RequestMapping.class, Plugin.class })
@ConditionalOnWebApplication @ConditionalOnWebApplication
@AutoConfigureAfter(WebMvcAutoConfiguration.class) @AutoConfigureAfter({ WebMvcAutoConfiguration.class, JacksonAutoConfiguration.class })
public class HypermediaAutoConfiguration { public class HypermediaAutoConfiguration {
@Configuration @Configuration
@ConditionalOnMissingBean(LinkDiscoverers.class) @ConditionalOnMissingBean(LinkDiscoverers.class)
@EnableHypermediaSupport(type = HypermediaType.HAL) @EnableHypermediaSupport(type = HypermediaType.HAL)
protected static class HypermediaConfiguration { protected static class HypermediaConfiguration {
@ConditionalOnClass({ Jackson2ObjectMapperBuilder.class, ObjectMapper.class })
protected static class HalObjectMapperConfiguration {
@Autowired(required = false)
private Jackson2ObjectMapperBuilder objectMapperBuilder;
@Bean
public BeanPostProcessor halObjectMapperConfigurer() {
return new BeanPostProcessor() {
@Override
public Object postProcessAfterInitialization(Object bean,
String beanName) throws BeansException {
if (HalObjectMapperConfiguration.this.objectMapperBuilder != null
&& bean instanceof ObjectMapper
&& "_halObjectMapper".equals(beanName)) {
HalObjectMapperConfiguration.this.objectMapperBuilder
.configure((ObjectMapper) bean);
}
return bean;
}
@Override
public Object postProcessBeforeInitialization(Object bean,
String beanName) throws BeansException {
return bean;
}
};
}
}
} }
@Configuration @Configuration
......
...@@ -18,6 +18,8 @@ package org.springframework.boot.autoconfigure.hateoas; ...@@ -18,6 +18,8 @@ package org.springframework.boot.autoconfigure.hateoas;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.hateoas.EntityLinks; import org.springframework.hateoas.EntityLinks;
import org.springframework.hateoas.LinkDiscoverer; import org.springframework.hateoas.LinkDiscoverer;
...@@ -28,6 +30,9 @@ import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType ...@@ -28,6 +30,9 @@ import org.springframework.hateoas.config.EnableHypermediaSupport.HypermediaType
import org.springframework.hateoas.hal.HalLinkDiscoverer; import org.springframework.hateoas.hal.HalLinkDiscoverer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
...@@ -78,6 +83,20 @@ public class HypermediaAutoConfigurationTests { ...@@ -78,6 +83,20 @@ public class HypermediaAutoConfigurationTests {
this.context.getBean(LinkDiscoverers.class); this.context.getBean(LinkDiscoverers.class);
} }
@Test
public void jacksonConfigurationIsAppliedToTheHalObjectMapper() {
this.context = new AnnotationConfigWebApplicationContext();
this.context.register(HypermediaAutoConfiguration.class,
JacksonAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context,
"spring.jackson.serialization.INDENT_OUTPUT:true");
this.context.refresh();
ObjectMapper objectMapper = this.context.getBean("_halObjectMapper",
ObjectMapper.class);
assertTrue(objectMapper.getSerializationConfig().isEnabled(
SerializationFeature.INDENT_OUTPUT));
}
@Configuration @Configuration
@EnableHypermediaSupport(type = HypermediaType.HAL) @EnableHypermediaSupport(type = HypermediaType.HAL)
static class SampleConfig { static class SampleConfig {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment