简体   繁体   English

java.lang.ClassCastException:类模型 - 在主题中将 JSON 对象作为消息发送时,springboot kafka 集成出错

[英]java.lang.ClassCastException: class model - Error in springboot kafka integration while sending JSON object as message in topic

I am new to Kafka and am facing the below issue for mymodel class User [ Request processing failed;我是 Kafka 的新手,我的模型类用户面临以下问题 [请求处理失败; nested exception is org.apache.kafka.common.errors.SerializationException: Can't convert value of class model.User to class org.apache.kafka.common.serialization.StringSerializer specified in value.serializer] with root cause java.lang.ClassCastException: class model.User cannot be cast to class java.lang.String (model.User is in unnamed module of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap') at org.apache.kafka.common.serialization.StringSerializer.serialize(StringSerializer.java:28) ~[kafka-clients-2.7.1.jar:na] at *嵌套异常是 org.apache.kafka.common.errors.SerializationException:无法将类 model.User 的值转换为在 value.serializer] 中指定的类 org.apache.kafka.common.serialization.StringSerializer,其根本原因是 java.lang .ClassCastException: class model.User 不能转换为类 java.lang.String(model.User 位于加载器“app”的未命名模块中;java.lang.String 位于加载器“bootstrap”的模块 java.base 中)在 org .apache.kafka.common.serialization.StringSerializer.serialize(StringSerializer.java:28) ~[kafka-clients-2.7.1.jar:na] at *

I am suspecting it to be due to wrong imports of StringSerializer and JSONSerializer in KafkaConfiguration .Below is my code我怀疑这是由于在 KafkaConfiguration 中错误导入了 StringSerializer 和 JSONSerializer 。下面是我的代码

1- KafkaConfiguration 1- Kafka配置

package config;

import java.util.HashMap;
import java.util.Map;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.connect.json.JsonSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

import com.fasterxml.jackson.databind.ser.std.StringSerializer;

import model.User;

@Configuration
public class KafkaConfiguration {
    
    @Bean
    public ProducerFactory<String,User> producerFactory()
    {
        Map<String,Object> config=new HashMap<>();
        
        config.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"127.0.0.1:9092");
        config.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,StringSerializer.class);
        config.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,JsonSerializer.class);
        return new DefaultKafkaProducerFactory<>(config);
    }
    
    @Bean
    public KafkaTemplate<String,User> kafkaTemplate()
    {
        return new KafkaTemplate<>(producerFactory());
    }

}

2- UserResource class 2- UserResource 类

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import model.User;

@RestController
@RequestMapping("kafka")
public class UserResource {

    @Autowired
    KafkaTemplate<String,User> kafkatemplate;
    public static final String TOPIC="Kafka_Example";

    
    @GetMapping("/publish/{name}")
    public String postMessage(@PathVariable("name") final String name)
    {
        
    kafkatemplate.send(TOPIC,new User(name,"Technology",12000L));
    
    return "Published successfully";
    }
}

3- User class 3-用户类

package model;

public class User {

    private String name;
    private String dept;
    private long salary;
    
    
    public User(String name, String dept, long salary) {
        super();
        this.name = name;
        this.dept = dept;
        this.salary = salary;
    }
    

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getDept() {
        return dept;
    }
    public void setDept(String dept) {
        this.dept = dept;
    }
    public long getSalary() {
        return salary;
    }
    public void setSalary(long salary) {
        this.salary = salary;
    }
    
    
    
}

Can anyone please let me know where I am going wrong?Is it something regarding to imports(if so,what are the correct ones)?任何人都可以请让我知道我哪里出错了?这是否与进口有关(如果是,正确的是什么)?

Thanks谢谢

Solution解决方案

  1. You need to import string serializer and json serializer with this class您需要使用此类导入字符串序列化程序和 json 序列化程序
 org.apache.kafka.common.serialization.StringSerializer
 org.springframework.kafka.support.serializer.JsonSerializer
  1. Package name of you model class must be same with your controller class and spring application main class.您的模型类的包名称必须与您的控制器类和 spring 应用程序主类相同。

I follow these two things , issue has been resolved我遵循这两件事,问题已解决

Your code correct but you import wrong StringSerializer use below import您的代码正确,但您在导入下方使用了错误的 StringSerializer

org.apache.kafka.common.serialization.StringSerializer
org.springframework.kafka.support.serializer.JsonSerializer

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM