简体   繁体   中英

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

I am suspecting it to be due to wrong imports of StringSerializer and JSONSerializer in KafkaConfiguration .Below is my code

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 class

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

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
 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.

I follow these two things , issue has been resolved

Your code correct but you import wrong StringSerializer use below import

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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