You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/reference/antora/modules/ROOT/pages/codec.adoc
+65-1Lines changed: 65 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -39,10 +39,11 @@ See the https://docs.spring.io/spring-integration/api/org/springframework/integr
39
39
[[kryo]]
40
40
== Kryo
41
41
42
-
Currently, this is the only implementation of `Codec`, and it provides two kinds of `Codec`:
42
+
Currently, this is the only implementation of `Codec`, and it provides three kinds of `Codec`:
43
43
44
44
* `PojoCodec`: Used in the transformers
45
45
* `MessageCodec`: Used in the `CodecMessageConverter`
46
+
* `CompositeCodec`: Used in transformers
46
47
47
48
The framework provides several custom serializers:
48
49
@@ -53,6 +54,69 @@ The framework provides several custom serializers:
53
54
The first can be used with the `PojoCodec` by initializing it with the `FileKryoRegistrar`.
54
55
The second and third are used with the `MessageCodec`, which is initialized with the `MessageKryoRegistrar`.
55
56
57
+
[[composite-codec]]
58
+
=== CompositeCodec
59
+
The CompositeCodec is a codec that combines multiple codecs into a single codec, delegating encoding and decoding operations to the appropriate type-specific codec.
60
+
This implementation associates object types with their appropriate codecs while providing a fallback default codec for unregistered types.
61
+
62
+
An example implementation can be seen below:
63
+
```java
64
+
void encodeDecodeSample() {
65
+
Codec codec = getFullyQualifiedCodec();
66
+
67
+
//Encode and Decode a Dog Object
68
+
Dog dog = new Dog("Wolfy", 3, "woofwoof");
69
+
dog = codec.decode(
70
+
codec.encode(dog),
71
+
Dog.class);
72
+
System.out.println(dog);
73
+
74
+
//Encode and Decode a Cat Object
75
+
Cat cat = new Cat("Kitty", 2, 8);
76
+
cat = codec.decode(
77
+
codec.encode(cat),
78
+
Cat.class);
79
+
System.out.println(cat);
80
+
81
+
//Use the default code if the type being decoded and encoded is not Cat or dog.
82
+
Animal animal = new Animal("Badger", 5);
83
+
Animal animalOut = codec.decode(
84
+
codec.encode(animal),
85
+
Animal.class);
86
+
System.out.println(animalOut);
87
+
}
88
+
89
+
/**
90
+
* Create and return a {@link CompositeCodec} that associates {@code Dog} and {@code Cat}
91
+
* classes with their respective {@link PojoCodec} instances, while providing a default
92
+
* codec for {@code Animal} types.
93
+
* <p>
94
+
* @return a fully qualified {@link CompositeCodec} for {@code Dog}, {@code Cat},
95
+
* and fallback for {@code Animal}
96
+
*/
97
+
static Codec getFullyQualifiedCodec() {
98
+
Map<Class<?>, Codec> codecs = new HashMap<Class<?>, Codec>();
99
+
codecs.put(Dog.class, new PojoCodec(new KryoClassListRegistrar(Dog.class)));
100
+
codecs.put(Cat.class, new PojoCodec(new KryoClassListRegistrar(Cat.class)));
101
+
return new CompositeCodec(codecs, new PojoCodec(
102
+
new KryoClassListRegistrar(Animal.class)));
103
+
}
104
+
105
+
// Records that will be encoded and decoded in this sample
106
+
record Dog(String name, int age, String tag) {}
107
+
record Cat(String name, int age, int lives) {}
108
+
record Animal(String name, int age){}
109
+
```
110
+
111
+
In some cases a single type of object may return multiple codecs. In these cases an `IllegalStateException` is thrown.
112
+
113
+
NOTE: This class uses `ClassUtils.findClosestMatch` to select the appropriate codec for a given object type.
114
+
When multiple codecs match an object type, `ClassUtils.findClosestMatch` offers the `failOnTie` option.
115
+
If `failOnTie` is `false`, it will return any one of the matching codecs.
116
+
If `failOnTie` is `true` and multiple codecs match, it will throw an `IllegalStateException`.
117
+
CompositeCodec` sets `failOnTie` to `true`, so if multiple codecs match, an
0 commit comments