Make a globally defined ObjectMapper truly optional

This commit is contained in:
Marius Bogoevici
2016-04-06 19:54:08 -04:00
parent 9a92bda9c1
commit d2e00610cd
3 changed files with 24 additions and 3 deletions

View File

@@ -19,22 +19,28 @@ import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.cloud.stream.annotation.Bindings;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.converter.CompositeMessageConverterFactory;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.context.annotation.PropertySource;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.converter.CompositeMessageConverter;
import org.springframework.messaging.converter.MessageConverter;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.tuple.Tuple;
import org.springframework.util.MimeTypeUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
@@ -51,6 +57,9 @@ public class MessageChannelConfigurerTests {
@Autowired @Bindings(TestSink.class)
private Sink testSink;
@Autowired
private CompositeMessageConverterFactory messageConverterFactory;
@Autowired
private ObjectMapper objectMapper;
@@ -73,8 +82,18 @@ public class MessageChannelConfigurerTests {
}
@Test
public void testObjectMapperConfig() {
assertTrue("SerializationFeature 'WRITE_DATES_AS_TIMESTAMPS' should be disabled", !objectMapper.getSerializationConfig().isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS));
public void testObjectMapperConfig() throws Exception {
CompositeMessageConverter compositeMessageConverter = messageConverterFactory.getMessageConverterForType(MimeTypeUtils.APPLICATION_JSON);
List<MessageConverter> converters = compositeMessageConverter.getConverters();
for (MessageConverter converter : converters) {
DirectFieldAccessor converterAccessor = new DirectFieldAccessor(converter);
ObjectMapper objectMapper = (ObjectMapper) converterAccessor.getPropertyValue("objectMapper");
// assert that the ObjectMapper used by the converters is compliant with the Boot configuration
assertTrue("SerializationFeature 'WRITE_DATES_AS_TIMESTAMPS' should be disabled",
!objectMapper.getSerializationConfig().isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS));
// assert that the globally set bean is used by the converters
assertTrue(objectMapper == this.objectMapper);
}
}
@EnableBinding(Sink.class)

View File

@@ -85,7 +85,7 @@ public class ChannelBindingServiceConfiguration {
@Autowired
private MessageBuilderFactory messageBuilderFactory;
@Lazy @Autowired(required = false)
@Autowired(required = false)
private ObjectMapper objectMapper;
/**

View File

@@ -21,6 +21,7 @@ import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.convert.converter.Converter;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
@@ -55,6 +56,7 @@ public class SpelExpressionConverterConfiguration {
@Autowired
@Qualifier(IntegrationContextUtils.INTEGRATION_EVALUATION_CONTEXT_BEAN_NAME)
@Lazy
private EvaluationContext evaluationContext;
@Override