Polish Samples

This commit is contained in:
Gary Russell
2021-03-05 12:59:15 -05:00
parent 1cafb9f381
commit b399f2996e
6 changed files with 8 additions and 6 deletions

View File

@@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

View File

@@ -2,7 +2,7 @@
This sample demonstrates a simple producer and a multi-method consumer; the producer sends objects of types `Foo1` and `Bar1` and the consumer receives objects of type `Foo2` and `Bar2` (the objects have the same field, `foo`).
The producer uses a `JsonSerializer`; the consumer uses the `StringDeserializer` that is automatically configured by Spring Boot, together with a `StringJsonMessageConverter` which converts to the required type of the listener method argument.
The producer uses a `JsonSerializer`; the consumer uses a `ByteArrayDeserializer`, together with a `ByteArrayJsonMessageConverter` which converts to the required type of the listener method argument.
We can't infer the type in this case (because the type is used to choose the method to call).
We therefore configure type mapping on the producer and consumer side.
See the `application.yml` for the producer side and the `converter` bean on the consumer side.

View File

@@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

View File

@@ -29,10 +29,10 @@ import org.springframework.context.annotation.Profile;
import org.springframework.kafka.core.KafkaOperations;
import org.springframework.kafka.listener.DeadLetterPublishingRecoverer;
import org.springframework.kafka.listener.SeekToCurrentErrorHandler;
import org.springframework.kafka.support.converter.ByteArrayJsonMessageConverter;
import org.springframework.kafka.support.converter.DefaultJackson2JavaTypeMapper;
import org.springframework.kafka.support.converter.Jackson2JavaTypeMapper.TypePrecedence;
import org.springframework.kafka.support.converter.RecordMessageConverter;
import org.springframework.kafka.support.converter.StringJsonMessageConverter;
import org.springframework.util.backoff.FixedBackOff;
import com.common.Bar2;
@@ -63,7 +63,7 @@ public class Application {
@Bean
public RecordMessageConverter converter() {
StringJsonMessageConverter converter = new StringJsonMessageConverter();
ByteArrayJsonMessageConverter converter = new ByteArrayJsonMessageConverter();
DefaultJackson2JavaTypeMapper typeMapper = new DefaultJackson2JavaTypeMapper();
typeMapper.setTypePrecedence(TypePrecedence.TYPE_ID);
typeMapper.addTrustedPackages("com.common");

View File

@@ -4,3 +4,5 @@ spring:
value-serializer: org.springframework.kafka.support.serializer.JsonSerializer
properties:
spring.json.type.mapping: foo:com.common.Foo1,bar:com.common.Bar1
consumer:
value-deserializer: org.apache.kafka.common.serialization.ByteArrayDeserializer

View File

@@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>