Skip to content

Commit 58ce767

Browse files
authored
Merge pull request #283 from spring-cloud/jackson3-support
Move To Jackson 3
2 parents 3c6509b + 55f6581 commit 58ce767

File tree

10 files changed

+51
-44
lines changed

10 files changed

+51
-44
lines changed

spring-cloud-bus-tests/src/test/java/org/springframework/cloud/bus/jackson/BusJacksonIntegrationTests.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.Set;
2525
import java.util.concurrent.ConcurrentHashMap;
2626

27-
import com.fasterxml.jackson.databind.SerializationFeature;
2827
import org.junit.jupiter.api.Test;
2928

3029
import org.springframework.beans.factory.annotation.Autowired;
@@ -47,9 +46,9 @@
4746

4847
import static org.assertj.core.api.Assertions.assertThat;
4948
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
49+
import static tools.jackson.databind.cfg.DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS;
5050

51-
@SpringBootTest(properties = "spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS:true",
52-
webEnvironment = RANDOM_PORT)
51+
@SpringBootTest(properties = "spring.jackson.datetime.write-dates-as-timestamps=true", webEnvironment = RANDOM_PORT)
5352
@DirtiesContext
5453
public class BusJacksonIntegrationTests {
5554

@@ -68,9 +67,8 @@ public void testCustomEventSerializes() {
6867
assertThat(this.converter.isMapperCreated()).isFalse();
6968

7069
// set by configuration
71-
assertThat(this.converter.getMapper()
72-
.getSerializationConfig()
73-
.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)).isTrue();
70+
assertThat(this.converter.getMapper().build().serializationConfig().isEnabled(WRITE_DATES_AS_TIMESTAMPS))
71+
.isTrue();
7472

7573
Map map = this.rest.getForObject("http://localhost:" + this.port + "/date", Map.class);
7674
assertThat(map).containsOnlyKeys("date");

spring-cloud-bus/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<artifactId>spring-integration-core</artifactId>
4848
</dependency>
4949
<dependency>
50-
<groupId>com.fasterxml.jackson.dataformat</groupId>
50+
<groupId>tools.jackson.dataformat</groupId>
5151
<artifactId>jackson-dataformat-cbor</artifactId>
5252
<optional>true</optional>
5353
</dependency>

spring-cloud-bus/src/main/java/org/springframework/cloud/bus/BusEnvironmentPostProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121

22+
import org.springframework.boot.EnvironmentPostProcessor;
2223
import org.springframework.boot.SpringApplication;
23-
import org.springframework.boot.env.EnvironmentPostProcessor;
2424
import org.springframework.cloud.commons.util.IdUtils;
2525
import org.springframework.cloud.function.context.FunctionProperties;
2626
import org.springframework.core.env.ConfigurableEnvironment;

spring-cloud-bus/src/main/java/org/springframework/cloud/bus/event/RemoteApplicationEvent.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ public abstract class RemoteApplicationEvent extends ApplicationEvent {
4141

4242
protected static final PathDestinationFactory DEFAULT_DESTINATION_FACTORY = new PathDestinationFactory();
4343

44-
private final String originService;
44+
private String originService;
4545

46-
private final String destinationService;
46+
private String destinationService;
4747

48-
private final String id;
48+
private String id;
4949

5050
protected RemoteApplicationEvent() {
5151
// for serialization libs like jackson

spring-cloud-bus/src/main/java/org/springframework/cloud/bus/jackson/BusJacksonAutoConfiguration.java

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121
import java.util.List;
2222
import java.util.Set;
2323

24-
import com.fasterxml.jackson.databind.ObjectMapper;
25-
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
26-
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
2724
import org.apache.commons.logging.Log;
2825
import org.apache.commons.logging.LogFactory;
26+
import tools.jackson.databind.ObjectMapper;
27+
import tools.jackson.databind.cfg.MapperBuilder;
28+
import tools.jackson.databind.exc.InvalidTypeIdException;
29+
import tools.jackson.databind.json.JsonMapper;
30+
import tools.jackson.dataformat.cbor.CBORFactory;
31+
import tools.jackson.dataformat.cbor.CBORMapper;
2932

3033
import org.springframework.beans.factory.InitializingBean;
3134
import org.springframework.beans.factory.annotation.Autowired;
@@ -76,8 +79,7 @@ protected static class CborConfiguration {
7679

7780
@Bean
7881
public AbstractMessageConverter busCborConverter() {
79-
return new BusJacksonMessageConverter(new MimeType("application", "cbor"),
80-
new ObjectMapper(new CBORFactory()));
82+
return new BusJacksonMessageConverter(new MimeType("application", "cbor"), CBORMapper.builder().build());
8183
}
8284

8385
}
@@ -90,7 +92,7 @@ class BusJacksonMessageConverter extends AbstractMessageConverter implements Ini
9092

9193
private static final String DEFAULT_PACKAGE = ClassUtils.getPackageName(RemoteApplicationEvent.class);
9294

93-
private final ObjectMapper mapper;
95+
private final MapperBuilder mapperBuilder;
9496

9597
private final boolean mapperCreated;
9698

@@ -110,11 +112,11 @@ private BusJacksonMessageConverter() {
110112
super(mimeType);
111113

112114
if (objectMapper != null) {
113-
this.mapper = objectMapper;
115+
this.mapperBuilder = objectMapper.rebuild();
114116
this.mapperCreated = false;
115117
}
116118
else {
117-
this.mapper = new ObjectMapper();
119+
this.mapperBuilder = JsonMapper.builder();
118120
this.mapperCreated = true;
119121
}
120122
}
@@ -123,8 +125,8 @@ private BusJacksonMessageConverter() {
123125
return this.mapperCreated;
124126
}
125127

126-
/* for testing */ ObjectMapper getMapper() {
127-
return this.mapper;
128+
/* for testing */ MapperBuilder getMapper() {
129+
return this.mapperBuilder;
128130
}
129131

130132
public void setPackagesToScan(String[] packagesToScan) {
@@ -168,21 +170,22 @@ protected boolean supports(Class<?> aClass) {
168170

169171
@Override
170172
public Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
173+
ObjectMapper mapper = this.mapperBuilder.build();
171174
Object result = null;
172175
try {
173176
Object payload = message.getPayload();
174177

175178
if (payload instanceof byte[]) {
176179
try {
177-
result = this.mapper.readValue((byte[]) payload, targetClass);
180+
result = mapper.readValue((byte[]) payload, targetClass);
178181
}
179182
catch (InvalidTypeIdException e) {
180183
return new UnknownRemoteApplicationEvent(new Object(), e.getTypeId(), (byte[]) payload);
181184
}
182185
}
183186
else if (payload instanceof String) {
184187
try {
185-
result = this.mapper.readValue((String) payload, targetClass);
188+
result = mapper.readValue((String) payload, targetClass);
186189
}
187190
catch (InvalidTypeIdException e) {
188191
return new UnknownRemoteApplicationEvent(new Object(), e.getTypeId(),
@@ -203,8 +206,8 @@ else if (payload instanceof RemoteApplicationEvent) {
203206
}
204207

205208
@Override
206-
public void afterPropertiesSet() throws Exception {
207-
this.mapper.registerModule(new SubtypeModule(findSubTypes()));
209+
public void afterPropertiesSet() {
210+
this.mapperBuilder.subtypeResolver().registerSubtypes(findSubTypes());
208211
}
209212

210213
}

spring-cloud-bus/src/main/java/org/springframework/cloud/bus/jackson/SubtypeModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.cloud.bus.jackson;
1818

19-
import com.fasterxml.jackson.databind.module.SimpleModule;
19+
import tools.jackson.databind.module.SimpleModule;
2020

2121
/**
2222
* @author Spencer Gibb
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Environment Post Processor
2-
org.springframework.boot.env.EnvironmentPostProcessor=\
2+
org.springframework.boot.EnvironmentPostProcessor=\
33
org.springframework.cloud.bus.BusEnvironmentPostProcessor

spring-cloud-bus/src/test/java/org/springframework/cloud/bus/jackson/RemoteApplicationEventScanTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
import java.util.LinkedHashSet;
2222
import java.util.List;
2323

24-
import com.fasterxml.jackson.databind.ObjectMapper;
25-
import com.fasterxml.jackson.databind.jsontype.NamedType;
2624
import org.junit.Test;
2725
import test.foo.bar.FooBarTestRemoteApplicationEvent;
26+
import tools.jackson.databind.cfg.MapperBuilder;
27+
import tools.jackson.databind.jsontype.NamedType;
2828

2929
import org.springframework.boot.Banner;
3030
import org.springframework.boot.WebApplicationType;
@@ -96,11 +96,11 @@ private ConfigurableApplicationContext createTestContext(Class<?> configuration)
9696

9797
private void assertConverterBeanAfterPropertiesSet(final String[] expectedPackageToScan,
9898
final Class<?>... expectedRegisterdClasses) {
99-
final ObjectMapper mapper = (ObjectMapper) ReflectionTestUtils.getField(this.converter, "mapper");
99+
final MapperBuilder mapper = this.converter.getMapper();
100100

101101
@SuppressWarnings("unchecked")
102102
final LinkedHashSet<NamedType> registeredSubtypes = (LinkedHashSet<NamedType>) ReflectionTestUtils
103-
.getField(mapper.getSubtypeResolver(), "_registeredSubtypes");
103+
.getField(mapper.subtypeResolver(), "_registeredSubtypes");
104104

105105
final List<Class<?>> expectedRegisterdClassesAsList = new ArrayList<>(Arrays.asList(expectedRegisterdClasses));
106106
addStandardSpringCloudEventBusEvents(expectedRegisterdClassesAsList);

spring-cloud-bus/src/test/java/org/springframework/cloud/bus/jackson/SerializationTests.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818

1919
import java.util.Collections;
2020

21-
import com.fasterxml.jackson.databind.ObjectMapper;
2221
import org.junit.Test;
22+
import tools.jackson.databind.MapperFeature;
23+
import tools.jackson.databind.ObjectMapper;
24+
import tools.jackson.databind.json.JsonMapper;
2325

2426
import org.springframework.cloud.bus.event.EnvironmentChangeRemoteApplicationEvent;
2527
import org.springframework.cloud.bus.event.RefreshRemoteApplicationEvent;
@@ -33,12 +35,15 @@
3335
*/
3436
public class SerializationTests {
3537

36-
ObjectMapper mapper = new ObjectMapper();
38+
ObjectMapper mapper;
3739

3840
@Test
3941
public void vanillaDeserialize() throws Exception {
40-
this.mapper.registerModule(
41-
new SubtypeModule(RefreshRemoteApplicationEvent.class, EnvironmentChangeRemoteApplicationEvent.class));
42+
this.mapper = JsonMapper.builder()
43+
.addModule(new SubtypeModule(RefreshRemoteApplicationEvent.class,
44+
EnvironmentChangeRemoteApplicationEvent.class))
45+
.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true)
46+
.build();
4247
EnvironmentChangeRemoteApplicationEvent source = new EnvironmentChangeRemoteApplicationEvent(this, "foo", "bar",
4348
Collections.<String, String>emptyMap());
4449
String value = this.mapper.writeValueAsString(source);
@@ -50,8 +55,10 @@ public void vanillaDeserialize() throws Exception {
5055

5156
@Test
5257
public void deserializeOldValueWithNoId() throws Exception {
53-
this.mapper.registerModule(
54-
new SubtypeModule(RefreshRemoteApplicationEvent.class, EnvironmentChangeRemoteApplicationEvent.class));
58+
this.mapper = JsonMapper.builder()
59+
.addModule(new SubtypeModule(RefreshRemoteApplicationEvent.class,
60+
EnvironmentChangeRemoteApplicationEvent.class))
61+
.build();
5562
EnvironmentChangeRemoteApplicationEvent source = new EnvironmentChangeRemoteApplicationEvent(this, "foo", "bar",
5663
Collections.<String, String>emptyMap());
5764
String value = this.mapper.writeValueAsString(source);

spring-cloud-bus/src/test/java/org/springframework/cloud/bus/jackson/SubtypeModuleTests.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717
package org.springframework.cloud.bus.jackson;
1818

1919
import com.fasterxml.jackson.annotation.JsonTypeName;
20-
import com.fasterxml.jackson.databind.ObjectMapper;
21-
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
2220
import org.junit.Test;
21+
import tools.jackson.databind.ObjectMapper;
22+
import tools.jackson.databind.json.JsonMapper;
2323

2424
import org.springframework.cloud.bus.event.AckRemoteApplicationEvent;
2525
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
@@ -38,8 +38,7 @@ public class SubtypeModuleTests {
3838

3939
@Test
4040
public void testDeserializeSubclass() throws Exception {
41-
ObjectMapper mapper = new ObjectMapper();
42-
mapper.registerModule(new SubtypeModule(MyRemoteApplicationEvent.class));
41+
ObjectMapper mapper = JsonMapper.builder().addModule(new SubtypeModule(MyRemoteApplicationEvent.class)).build();
4342

4443
RemoteApplicationEvent event = mapper.readValue(
4544
"{\"type\":\"my\", \"destinationService\":\"myservice\", \"originService\":\"myorigin\"}",
@@ -60,10 +59,10 @@ public void testDeserializeWhenTypeIsKnown() throws Exception {
6059

6160
@Test
6261
public void testDeserializeCustomizedObjectMapper() throws Exception {
63-
ObjectMapper mapper = new ObjectMapper();
64-
mapper.setPropertyNamingStrategy(new PropertyNamingStrategies.SnakeCaseStrategy());
62+
JsonMapper.Builder mapper = JsonMapper.builder()
63+
.propertyNamingStrategy(tools.jackson.databind.PropertyNamingStrategies.SNAKE_CASE);
6564

66-
BusJacksonMessageConverter converter = new BusJacksonMessageConverter(mapper);
65+
BusJacksonMessageConverter converter = new BusJacksonMessageConverter(mapper.build());
6766
converter.afterPropertiesSet();
6867
Object event = converter.fromMessage(
6968
MessageBuilder.withPayload("{\"type\":\"TestRemoteApplicationEvent\", \"origin_service\":\"myorigin\"}")

0 commit comments

Comments
 (0)