Kafka + SpringData + (Avro & String) 【Can't convert value of class java.lang.String】问题解决
【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】问题解决的更多相关文章
- 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 ...
- 解决spring mvc 上传报错,Field [] isn't an enum value,Failed to convert value of type 'java.lang.String[]' to required type '
没有选择附件,但是点击上传按钮的时候会报错. 之前不选择文件,直接上传空文件是可以的,后来不知道改了什么就不行了. 错误信息: -- :: [http--] TRACE org.springframe ...
- 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 ...
- 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 ...
- 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 ...
- 完美解决报错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' 首先这个错误的意思是 前台页面 ...
- 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] ...
- 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 ...
- 【Java面试题】53 能不能自己写个类,也叫java.lang.String?
可以,但是即使你写了这个类,也没有用. 这个问题涉及到加载器的委托机制,在类加载器的结构图(在下面)中,BootStrap是顶层父类,ExtClassLoader是BootStrap类的子类,ExtC ...
- 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 ...
随机推荐
- js操作网页元素
二.操作网页元素 1.查找网页元素 给标签设置id属性,一个网页中id的值不能出现重复的 <button id="btn">按钮</button> 2.给按 ...
- 在Unity3D中开发的坦克履带模拟器Tank Track Simulator
为了在Unity游戏中比较真实地模拟坦克履带的运动,本人便开发了这款Tank Track Simulator插件 特点 比较真实地模拟了坦克履带的运动. 本插件中包含了一辆M1A2坦克模型,已经将这个 ...
- PHP Redis - Set(集合)
Redis 的 set 无序集合,与 list 类似,特殊之处在于 set 可以自动排重,不会出现重复数据 集合中最大的成员数为 232-1 (4294967295, 每个集合可存储40多亿个成员). ...
- Vscode,php运行
1.下载好vscode,点击左侧扩展,然后搜索php,安装插件 2.打开小皮面板创建网站 点击确认 创建成功 3.浏览器输入http://myblog验证 4.在vscode打开新建的myblog文件 ...
- withRouter
withRouter 可以加工一般组件,让一般组件具备路由组件所特有的api,比如this.props.history withRouter的返回值是一个新组件 import {withRouter} ...
- 最短路算法之 Dijkstra
部分内容参考了李煜东的<算法竞赛进阶指南>,在此声明. 单源最短路径 单源最短路径问题,是说,给定一张有向图(无向图)\(G=(V,E)\) ,\(V\) 是点集,\(E\) 是边集,\( ...
- 如何卸载cad 2022?怎么把cad 2022彻底卸载删除干净重新安装的方法【转载】
标题:cad 2022重新安装方法经验总结,利用卸载清理工具完全彻底排查删除干净cad 2022各种残留注册表和文件.cad 2022显示已安装或者报错出现提示安装未完成某些产品无法安装的问题,怎么完 ...
- intellij idea中怎么没有git版本控制设置项
在使用intellij idea的时候想要使用git进行版本控制,但是在设置项和界面没有发现相关内容,怎么回事呢? 我们先打开电脑,从桌面的快捷方式打开intellij idea,进入到intelli ...
- Verilog 变量中位的数值类型
Verilog 变量中位的数值类型 Verilog变量中每个位(bit)的数值类型有四种,分别为1,0,Z,X.其中1,0比较明确就是高.低电平.而x, z在逻辑设计中也经常用到来建模.X,Z既可以大 ...
- jsp第7个作业
MailService package mail.service; import java.util.List; import mail.dao.DaoFactory; import mail.d ...