微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java.lang.ClassCastException: 类模型 - springboot kafka 集成出错,同时在主题

如何解决java.lang.ClassCastException: 类模型 - springboot kafka 集成出错,同时在主题

我是 Kafka 的新手,我面临着 mymodel 类用户的以下问题 [请求处理失败;嵌套异常是 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*

我怀疑这是由于在 KafkaConfiguration 中错误地导入了 StringSerializer 和 JSONSerializer 。下面是我的代码

1- KafkaConfiguration

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 类

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- 用户

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;
    }
    
    
    
}

谁能告诉我我哪里出错了?这是否与进口有关(如果是,正确的是什么)?

谢谢

解决方法

您的代码正确,但您在导入下方使用了错误的 StringSerializer

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。