0

我是 Kafka 的新手,我的模型类用户面临以下问题 [请求处理失败;嵌套异常是 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] 根本原因是 java.lang .ClassCastException:类 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] 在*

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

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 类

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

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

谢谢

4

2 回答 2

1

解决方案

  1. 您需要使用此类导入字符串序列化程序和 json 序列化程序
 org.apache.kafka.common.serialization.StringSerializer
 org.springframework.kafka.support.serializer.JsonSerializer
  1. 您的模型类的包名称必须与您的控制器类和 Spring 应用程序主类相同。

我遵循这两件事,问题已解决

于 2021-10-17T16:38:16.970 回答
0

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

org.apache.kafka.common.serialization.StringSerializer
org.springframework.kafka.support.serializer.JsonSerializer
于 2021-07-10T02:58:21.617 回答