diff --git a/spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/AbstractKryoCodec.java b/spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/AbstractKryoCodec.java index f3c28af5ee..425b551906 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/AbstractKryoCodec.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/AbstractKryoCodec.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2019 the original author or authors. + * Copyright 2015-2020 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. @@ -44,6 +44,7 @@ public abstract class AbstractKryoCodec implements Codec { protected AbstractKryoCodec() { KryoFactory factory = () -> { Kryo kryo = new Kryo(); + kryo.setRegistrationRequired(true); // configure Kryo instance, customize settings configureKryoInstance(kryo); return kryo; diff --git a/spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/KryoClassListRegistrar.java b/spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/KryoClassListRegistrar.java index 34aa81c917..fa95f4be56 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/KryoClassListRegistrar.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/KryoClassListRegistrar.java @@ -17,6 +17,7 @@ package org.springframework.integration.codec.kryo; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import org.springframework.util.Assert; @@ -39,6 +40,13 @@ public class KryoClassListRegistrar extends AbstractKryoRegistrar { private int initialValue = 50; + /** + * @param classes the vararg of classes to validateRegistration + */ + public KryoClassListRegistrar(Class... classes) { + this(Arrays.asList(classes)); + } + /** * @param classes the list of classes to validateRegistration */ @@ -52,8 +60,7 @@ public class KryoClassListRegistrar extends AbstractKryoRegistrar { * @param initialValue the initial value */ public void setInitialValue(int initialValue) { - Assert.isTrue(initialValue >= MIN_REGISTRATION_VALUE, - "'initialValue' must be >= " + MIN_REGISTRATION_VALUE); + Assert.isTrue(initialValue >= MIN_REGISTRATION_VALUE, "'initialValue' must be >= " + MIN_REGISTRATION_VALUE); this.initialValue = initialValue; } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/CompositeCodecTests.java b/spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/CompositeCodecTests.java index c1b0b3f928..a067439809 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/CompositeCodecTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/CompositeCodecTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2019 the original author or authors. + * Copyright 2015-2020 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. @@ -39,8 +39,9 @@ public class CompositeCodecTests { @Before public void setup() { - Map, Codec> codecs = new HashMap, Codec>(); - this.codec = new CompositeCodec(codecs, new PojoCodec()); + Map, Codec> codecs = new HashMap<>(); + this.codec = new CompositeCodec(codecs, new PojoCodec( + new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class))); } @Test diff --git a/spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/KryoCodecTests.java b/spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/KryoCodecTests.java index 12212db547..0ba5d69eb6 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/KryoCodecTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/codec/kryo/KryoCodecTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2015-2019 the original author or authors. + * Copyright 2015-2020 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. @@ -63,7 +63,7 @@ public class KryoCodecTests { @Test public void testPojoSerialization() throws IOException { - PojoCodec codec = new PojoCodec(); + PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(SomeClassWithNoDefaultConstructors.class)); SomeClassWithNoDefaultConstructors foo = new SomeClassWithNoDefaultConstructors("foo", 123); ByteArrayOutputStream bos = new ByteArrayOutputStream(); codec.encode(foo, bos); @@ -98,12 +98,12 @@ public class KryoCodecTests { @Test public void testMapSerialization() throws IOException { - PojoCodec codec = new PojoCodec(); + PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(HashMap.class)); Map map = new HashMap(); map.put("one", 1); map.put("two", 2); ByteArrayOutputStream bos = new ByteArrayOutputStream(); - codec.encode(map, bos); + codec.encode(map, bos);4 Map m2 = (Map) codec.decode(bos.toByteArray(), HashMap.class); assertEquals(2, m2.size()); assertEquals(1, m2.get("one")); @@ -112,7 +112,7 @@ public class KryoCodecTests { @Test public void testComplexObjectSerialization() throws IOException { - PojoCodec codec = new PojoCodec(); + PojoCodec codec = new PojoCodec(new KryoClassListRegistrar(Foo.class)); Foo foo = new Foo(); foo.put("one", 1); foo.put("two", 2);