传递和读取XML字符串Spring Cloud Stream

时间:2019-04-04 22:56:15

标签: java xml spring-boot spring-cloud-stream spring-kafka

我有一个xml

<?xml version = .......
<A>1</A>
<B>2</>
</>

我正在尝试将其转换为String并将其作为有效载荷通过Kafka发送给另一个应用程序

//制作人应用

@EnableBinding(Source.class)

public class Producer {


      @InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "7000", maxMessagesPerPoll = "1"))
        public Message produceMessages() {

           //Converting xml to String here
            BufferedReader br = new BufferedReader(new FileReader(new File(filename)));
            String line;
            StringBuilder sb = new StringBuilder();

            while((line=br.readLine())!= null){
                sb.append(line.trim());

            string1 = sb.toString();

            Message<string> msg1 = MessageBuilder.withPayload(string1).build();

}

App.yml

spring:
  application:
     name: some-app
  cloud:
    stream:
      defaultBinder: kafka
      bindings:
        output:
          destination: someTopic

//消费者

@EnableBinding(Interface1.class)
public class ConsumerApp {

    @StreamListener(Interface1.RECEIVE_FROM_SOURCE)
    public void handleMessage(Message message){

        String receivedPayload = message.getPayload().toString();
        logger.info(received payload >>> " +receivedPayload);

        someObject.someMethod(receivedPayload); ///this needs to have just xml string as parameter and throws error

    }
}

当我这样做时..

我知道

received payload >>> "{"payload":"<?xmlns....<A>1</A><B>2</B>"}

错误

java.lang.String cannot be cast to [Ljava.lang.Object;

我怎样才能只获取一个String的有效载荷值?谢谢。

0 个答案:

没有答案