【1】需求:Kafka 使用 Avero 反序列化时,同时需要对 String 类型的 JSON数据进行反序列化。AvroConfig的配置信息如下:

 1 /**
2 * @author zzx
3 * @creat 2020-03-11-20:23
4 */
5 @Configuration
6 @EnableKafka
7 public class AvroConfig {
8 //生产者配置 略。。。。 主要说消费者问题
9 //消费者配置 avro 反序列化如下 主要是注入 Bean 的名称不同
10 @Bean
11 public Map<String, Object> consumerConfigs() {
12 Map<String, Object> props = new HashMap<>();
13 props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
14 props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
15 //主要区别
16 props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, AvroDeserializer.class);
17 props.put(ConsumerConfig.GROUP_ID_CONFIG, "avro");
18 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"latest");
19 props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"true");
20 props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"1000");
21 return props;
22 }
23
24 @Bean
25 public ConsumerFactory<String, LatData> consumerFactory() {
26 return new DefaultKafkaConsumerFactory<>(consumerConfigs(), new StringDeserializer(),
27 new AvroDeserializer<>(LatData.class));
28 }
29
30 @Bean
31 public ConcurrentKafkaListenerContainerFactory<String, LatData> kafkaListenerContainerFactory() {
32 ConcurrentKafkaListenerContainerFactory<String, LatData> factory =
33 new ConcurrentKafkaListenerContainerFactory<>();
34 factory.setConsumerFactory(consumerFactory());
35 return factory;
36 }
37
38 //消费者配置 String 反序列化如下
39 @Bean
40 public Map<String, Object> stringConsumerConfigs() {
41 Map<String, Object> props = new HashMap<>();
42 props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
43 props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
44 //主要区别
45 props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
46 props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,"latest");
47 props.put(ConsumerConfig.ENABLE_AUTO_COMMIT_CONFIG,"true");
48 props.put(ConsumerConfig.AUTO_COMMIT_INTERVAL_MS_CONFIG,"1000");
49 return props;
50 }
51
52 @Bean
53 public DefaultKafkaConsumerFactory stringConsumerFactory() {
54 return new DefaultKafkaConsumerFactory<>(stringConsumerConfigs(), new StringDeserializer(),
55 new StringDeserializer());
56 }
57
58 @Bean
59 public ConcurrentKafkaListenerContainerFactory<String, Object> stringKafkaListenerContainerFactory() {
60 ConcurrentKafkaListenerContainerFactory<String, Object> factory =
61 new ConcurrentKafkaListenerContainerFactory<>();
62 factory.setConsumerFactory(stringConsumerFactory());
63 return factory;
64 }
65
66 }

【2】消费者监听:当直接使用  @KafkaListener(topics = {"xx"})时,出现反序列化错误问题。

@KafkaListener(topics = {LOADING_TOPIC_NAME})
public void revice(String data) throws Exception {

【3】问题: ERROR c.y.c.exception.BDExceptionHandler - Can't convert value of class java.lang.String to class com.yunda.common.utils.avro.AvroSerializer specified in value.serializer
org.apache.kafka.common.errors.SerializationException: Can't convert value of class java.lang.String to class com.yunda.common.utils.avro.AvroSerializer specified in value.serializer
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.avro.specific.SpecificRecordBase

ERROR c.y.c.exception.BDExceptionHandler - Can't convert value of class java.lang.String to class com.yunda.common.utils.avro.AvroSerializer specified in value.serializer
org.apache.kafka.common.errors.SerializationException: Can't convert value of class java.lang.String to class com.yunda.common.utils.avro.AvroSerializer specified in value.serializer
Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to org.apache.avro.specific.SpecificRecordBase

【4】问题解决:添加具体需要使用的容器 containerFactory 问题迎刃而解

1 @KafkaListener(topics = {ENTRY_TOPIC_NAME},containerFactory="stringkafkaListenerContainerFactory")
2 public void revice(String data) throws Exception {

Kafka + SpringData + (Avro & String) 【Can't convert value of class java.lang.String】问题解决的更多相关文章

  1. Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'xxx': no matching editors or conversion strategy found

    今天在完成项目的时候遇到了下面的异常信息: 04-Aug-2014 15:49:27.894 SEVERE [http-apr-8080-exec-5] org.apache.catalina.cor ...

  2. 解决spring mvc 上传报错,Field [] isn't an enum value,Failed to convert value of type 'java.lang.String[]' to required type '

    没有选择附件,但是点击上传按钮的时候会报错. 之前不选择文件,直接上传空文件是可以的,后来不知道改了什么就不行了. 错误信息: -- :: [http--] TRACE org.springframe ...

  3. Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';

    springboot jdbc查询使用LocalDate报:Failed to convert value of type 'java.lang.String' to required type 'j ...

  4. Cannot convert value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'dataSource': no matching editors or conversion strategy found

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFa ...

  5. SpringBoot 项目启动 Failed to convert value of type 'java.lang.String' to required type 'cn.com.goldenwater.dcproj.dao.TacPageOfficePblmListDao';

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'tac ...

  6. 完美解决报错Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'

    Failed to convert value of type 'java.lang.String' to required type 'java.util.Date' 首先这个错误的意思是 前台页面 ...

  7. spring mvc出现 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endtime'

    在使用spring mvc中,绑定页面传递时间字符串数据给Date类型是出错: Failed to convert property value of type [java.lang.String] ...

  8. Failed to convert from type [java.lang.String] to type [java.util.Date] for value '2020-02-06'; nested exception is java.lang.IllegalArgumentException]解决

    今天做springbook项目前端输入日期传到数据库保存报了一下错误 Whitelabel Error Page This application has no explicit mapping fo ...

  9. 【Java面试题】53 能不能自己写个类,也叫java.lang.String?

    可以,但是即使你写了这个类,也没有用. 这个问题涉及到加载器的委托机制,在类加载器的结构图(在下面)中,BootStrap是顶层父类,ExtClassLoader是BootStrap类的子类,ExtC ...

  10. mybatis之org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'time' in 'class java.lang.String'

    mybatis接口 List<String> getUsedCate(String time); 配置文件 <select id="getUsedCate" pa ...

随机推荐

  1. 【BOOK】【实例】【requests库+正则表达式】猫眼TOP100电影排名爬取

    猫眼电影TOP100页面爬取 https://maoyan.com/board/4 ##猫眼电影TOP100爬取 import requests import re import json impor ...

  2. iOS 高级面试题

    面试题 iOS 基础题 分类和扩展有什么区别?可以分别用来做什么?分类有哪些局限性?分类的结构体里面有哪些成员? 讲一下atomic的实现机制:为什么不能保证绝对的线程安全(最好可以结合场景来说)? ...

  3. 深入理解snp-calling流程——转载

    ------------恢复内容开始------------ GATK4流程 准备配套数据 明确参考基因组版本!!!b36/b37/hg18/hg19/hg38,记住b37和hg19并不是完全一样的, ...

  4. fastdfs 上传成功后返回了错误URL,Request URL: http://localhost:8081/121.122.25.133/group1/M00/00/00/wKgZhV63.jpg

    错误的URL. 原因: 图片服务器地址格式错误,fastdfs返回了错误的URL IMAGE_SERVER_URL = http:121.12.25.13/ 正确: IMAGE_SERVER_URL ...

  5. Android自动化测试

    一.环境搭建 必备软件: Windows或MacOS系统的电脑 python pycharm Android SDK(需要配置环境变量,网上有教程) Appium客户端(需要配置Android SDK ...

  6. SQL IIF函数的使用 判断为空数据不显示的问题

    先说说IIF函数 IIF函数 需要一个条件 两个值 当条件满足的时候 执行第一个值 条件不满足的时候 执行第二个值 IIF(判断条件,值1,值2) 今天判断数据的时候 发现当值为NULL或者为'    ...

  7. 解决element-ui表格字段用expand展开行时其他字段也会同时展开的问题

    使用element-ui的table表格时,我们通常会在需要展开的列上通过设置 type="expand" 和 Scoped slot 可以开启展开行功能,但是渲染结果会出现其他未 ...

  8. 怎样修改linux内核

    1.先查看linux内核 uname -a 2.打开内核配置文件 sudo vi /etc/default/grub 3.跟新grub文件 sudo update-grub 4.最后重启电脑 sudo ...

  9. vue移动端购物商场首页制作

    1.搭建项目框架 新建首页主组件及其子组件并将子组件展示出来 2.封装所需接口 3.编写轮播图组件 <template> <div id="swipercom"& ...

  10. ajax的重新学习,以及axios

    首先在这次学习之前已经又过了简单的入门,并且在上学期vue的开发中对其中的ajax语法以及方法有所认识,但是还有很多别的框架不同种的写法,因此我需要掌握原生ajax的知识 一.对ajax的简单认识 a ...