Merge branch '1.3.x'

This commit is contained in:
Spencer Gibb
2018-02-05 16:08:01 -05:00
2 changed files with 41 additions and 12 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2018 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.
@@ -25,6 +25,7 @@ import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -63,8 +64,8 @@ public class BusJacksonAutoConfiguration {
@Bean
@ConditionalOnMissingBean(name = "busJsonConverter")
@StreamMessageConverter
public AbstractMessageConverter busJsonConverter() {
return new BusJacksonMessageConverter();
public AbstractMessageConverter busJsonConverter(@Autowired(required = false) ObjectMapper objectMapper) {
return new BusJacksonMessageConverter(objectMapper);
}
}
@@ -77,10 +78,24 @@ class BusJacksonMessageConverter extends AbstractMessageConverter
private static final String DEFAULT_PACKAGE = ClassUtils
.getPackageName(RemoteApplicationEvent.class);
private final ObjectMapper mapper = new ObjectMapper();
private final ObjectMapper mapper;
private String[] packagesToScan = new String[] { DEFAULT_PACKAGE };
public BusJacksonMessageConverter() {
this(null);
}
public BusJacksonMessageConverter(ObjectMapper objectMapper) {
super(MimeTypeUtils.APPLICATION_JSON);
if (objectMapper != null) {
this.mapper = objectMapper;
} else {
this.mapper = new ObjectMapper();
}
}
public void setPackagesToScan(String[] packagesToScan) {
List<String> packages = new ArrayList<>(Arrays.asList(packagesToScan));
if (!packages.contains(DEFAULT_PACKAGE)) {
@@ -89,10 +104,6 @@ class BusJacksonMessageConverter extends AbstractMessageConverter
this.packagesToScan = packages.toArray(new String[0]);
}
public BusJacksonMessageConverter() {
super(MimeTypeUtils.APPLICATION_JSON);
}
private Class<?>[] findSubTypes() {
List<Class<?>> types = new ArrayList<>();
if (this.packagesToScan != null) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2017 the original author or authors.
* Copyright 2013-2018 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.
@@ -17,9 +17,6 @@
package org.springframework.cloud.bus.jackson;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.cloud.bus.event.AckRemoteApplicationEvent;
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
@@ -30,6 +27,11 @@ import org.springframework.messaging.support.MessageBuilder;
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Spencer Gibb
@@ -61,6 +63,22 @@ public class SubtypeModuleTests {
assertTrue("event is wrong type", event instanceof AnotherRemoteApplicationEvent);
}
@Test
public void testDeserializeCustomizedObjectMapper() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
BusJacksonMessageConverter converter = new BusJacksonMessageConverter(mapper);
converter.afterPropertiesSet();
Object event = converter.fromMessage(
MessageBuilder.withPayload("{\"type\":\"TestRemoteApplicationEvent\", \"origin_service\":\"myorigin\"}").build(),
RemoteApplicationEvent.class);
assertThat(event)
.isNotNull()
.isInstanceOf(TestRemoteApplicationEvent.class);
assertThat(TestRemoteApplicationEvent.class.cast(event).getOriginService()).isEqualTo("myorigin");
}
@Test
public void testDeserializeWithMessageConverter() throws Exception {
BusJacksonMessageConverter converter = new BusJacksonMessageConverter();