在春季将JSON有效负载发送到Apache Kafka主题

时间:2018-11-20 08:07:16

标签: java json apache-kafka kafka-producer-api spring-kafka

我需要将Json有效负载发送(发布)到Apache Kafka主题,但是我收到以下错误:-   “消息”:“无法将com.xyz.User类的值转换为value.serializer中指定的org.apache.kafka.common.serialization.StringSerializer类”

Spring也显示类强制转换异常:      java.lang.ClassCastException:com.xyz.User无法转换为java.lang.String

以下是我的模态,kafka配置和控制器

public class User {
    private String firstname;
    private String email;


    public User() {}
    public User(String firstname, String email) {
        super();
        this.firstname = firstname;
        this.email = email;
    }
    public String getFirstname() {
        return firstname;
    }
    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    @Override
    public String toString() {
        return "UserModel [firstname=" + firstname + ", email=" + email + "]";
    }


}

Kafka配置

 package config;

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

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
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 org.springframework.kafka.support.serializer.JsonSerializer;

import com.xyz.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());
        }

}

控制器

@RestController
@RequestMapping("/kafka")
public class UserController {
    @Autowired
    private KafkaTemplate<String, User> kafkaTemplate;

    private static String TOPIC = "kafka-producer";
    @PostMapping("/publish")
    public void getUserId(@RequestBody User user) {

        kafkaTemplate.send(TOPIC, user);

邮递员发送的Json有效载荷

{
    "firstname" : "xyz",
    "email" : "xyz@gmail.com.com"

}

4 个答案:

答案 0 :(得分:0)

在发送到KafkaTemplate之前,将您的User对象转换为生产者中的String。然后在使用者端将String转换为User对象。

制作人:

@PostMapping("/publish")
public void getUserId(@RequestBody User user) {
    kafkaTemplate.send(TOPIC, new ObjectMapper().writeValueAsString(user));
}

消费者:

User user = new ObjectMapper().readValue(kafkaMessage, User.class);

答案 1 :(得分:0)

以下为我工作。

public class UserController {
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;

private static String TOPIC = "kafka-producer";
@PostMapping("/publish")
public void getUserId(@RequestBody User user) {
    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    String json = null;
    try {
        json = ow.writeValueAsString(user);
    } catch (JsonProcessingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }   
kafkaTemplate.send(TOPIC, json);


}

答案 2 :(得分:0)

能够在我的本地设置中重现您的错误。

Caused by: org.apache.kafka.common.errors.SerializationException: Can't convert value of class com.example.demo.User to class org.apache.kafka.common.serialization.StringSerializer specified in value.serializer
Caused by: java.lang.ClassCastException: com.example.demo.User cannot be cast to java.lang.String

如果我们查看日志,则会显示value.serializer值仍指的是默认StringSerializer而不是预期的JsonSerializer,这反过来又说明您的生产者配置未生效。简而言之,您的KafkaCOnfiguration类没有被引用。

您的KafkaConfiguration类在某些config包中,而User类在某些com.xyz包中。因此,解决方案是确保它能够拾取您的配置。很有可能该软件包可能不会被扫描以获取配置/ bean定义。如果将KafkaConfiguration移至应用程序的根包,则原始代码应该可以正常工作。

如果您说您的KafkaTemplate对象被注入,则实际上不是。注入的一个由Spring kafka Autoconfiguration定义。

答案 3 :(得分:0)

服务

@Service
public class KafkaSenderService {

  @Autowired
  private KafkaTemplate<String, JsonNode> kafkaTemplate;

  public void send(String kafkaTopic, JsonNode message) {
    kafkaTemplate.send(kafkaTopic, message);
  }
}

控制器

for (JsonNode data: datas) {
   kafkaSender.send(topicName,data);
}

application.properties

spring.kafka.producer.value-serializer: org.springframework.kafka.support.serializer.JsonSerializer