2

我需要测试消息,其中包含标题,所以我需要使用MessageBuilder,但是我无法序列化。发送到kafka主题时序列化消息的错误

我试着在制作人道具上添加序列化设置,但没有奏效。

有人可以帮助我吗?

此错误:

org.apache.kafka.common.errors.SerializationException: Can't convert value of class org.springframework.messaging.support.GenericMessage to class org.apache.kafka.common.serialization.StringSerializer specified in value.serializer 

我的测试类:

public class TransactionMastercardAdapterTest extends AbstractTest{ 

@Autowired 
private KafkaTemplate<String, Message<String>> template; 

@ClassRule 
public static KafkaEmbedded embeddedKafka = new KafkaEmbedded(1); 

@BeforeClass 
public static void setUp() { 
    System.setProperty("spring.kafka.bootstrap-servers", embeddedKafka.getBrokersAsString()); 
    System.setProperty("spring.cloud.stream.kafka.binder.zkNodes", embeddedKafka.getZookeeperConnectionString()); 
} 

@Test 
public void sendTransactionCommandTest(){ 

    String payload = "{\"o2oTransactionId\" : \"" + UUID.randomUUID().toString().toUpperCase() + "\"," 
      + "\"cardId\" : \"11\"," 
      + "\"transactionId\" : \"20110405123456\"," 
      + "\"amount\" : 200.59," 
      + "\"partnerId\" : \"11\"}"; 

    Map<String, Object> props = KafkaTestUtils.producerProps(embeddedKafka); 
    props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer"); 
    props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer"); 

    Producer<String, Message<String>> producer = new KafkaProducer<>(props); 
    producer.send(new ProducerRecord<String, Message<String>> ("notification_topic", MessageBuilder.withPayload(payload) 
      .setHeader("status", "RECEIVED") 
      .setHeader("service", "MASTERCARD") 
      .build())); 

    Map<String, Object> configs = KafkaTestUtils.consumerProps("test1", "false", embeddedKafka); 

    configs.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); 
    ConsumerFactory<byte[], byte[]> cf = new DefaultKafkaConsumerFactory<>(configs); 

    Consumer<byte[], byte[]> consumer = cf.createConsumer(); 
    consumer.subscribe(Collections.singleton("transaction_topic")); 
    ConsumerRecords<byte[], byte[]> records = consumer.poll(10_000); 
    consumer.commitSync(); 

    assertThat(records.count()).isEqualTo(1); 
} 

}

回答

1

我想说的错误是显而易见的:

Can't convert value of class org.springframework.messaging.support.GenericMessage to class org.apache.kafka.common.serialization.StringSerializer specified in value.serializer 

如果你的价值b ut StringSerializer只能使用字符串。

你需要什么叫JavaSerializer不存在的,但不是那么难写:

public class JavaSerializer implements Serializer<Object> { 

    @Override 
    public byte[] serialize(String topic, Object data) { 
     try { 
      ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 
      ObjectOutputStream objectStream = new ObjectOutputStream(byteStream); 
      objectStream.writeObject(data); 
      objectStream.flush(); 
      objectStream.close(); 
      return byteStream.toByteArray(); 
     } 
     catch (IOException e) { 
      throw new IllegalStateException("Can't serialize object: " + data, e); 
     } 
    } 

    @Override 
    public void configure(Map<String, ?> configs, boolean isKey) { 

    } 

    @Override 
    public void close() { 

    } 

} 

,并将其配置为value.serializer财产。

+0

tks for answer,but not work,2017-04-25 11:11:12,133 ERROR -kafka-listener-1 oscsbkKafkaMessageChannelBinder:287 - 无法转换消息:ACED000573720 java.lang.StringIndexOutOfBoundsException:String index out范围:-19 –

+0

org.springframework.messaging.converter.MessageConversionException:无法读取JSON:意外字符('¬'(code 172)):预计有效值(数字,字符串,数组,对象,'true','假'或'空') 在[来源:[B @ 4e076253;行:1,列:2];嵌套异常是com.fasterxml.jackson.core.JsonParseException:意外字符('¬'(code 172)):预计有效值(数字,字符串,数组,对象,'true','false'或'null') at [来源:[B @ 4e076253;行:1,列:2] –

+0

我认为问题是GenericMessage类的2个属性,它创建2个jsons,头= {id = 067-879b0,服务= MASTER,状态= RECEIVED,时间戳= 1493130032409} 有效载荷=“{..}”。 也许这是不正确的方式发送消息头=/ –