Apache Camel没有可用的类型转换器

0lvr5msh  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(124)

我用camel定义了一个处理器,它允许我生成一个带有计时器的jaxb java bean,并将pojo写入xml文件。但是当我启动应用程序时,我得到了以下错误:
08:09:00 WARN [或.ap.ca.co.ti.TimerConsumer](Camel(camel-1)线程#2 -计时器://生成发票)处理交换时出错。交换[20 E715 FDB 7 EFE 19 - 000000000000000]。原因是:异常错误-无法进行类型转换。没有类型转换器可用于从类型转换:将java.util.LinkedList转换为所需类型:... class ='class10'输入流,并将该流转换为一个新的流,该新的流将被转换为一个新的流.
我的代号是惨叫:

from("timer:generateInvoice?period={{xml.timer.period}}&delay={{xml.timer.delay}}")
        .log("Generating randomized invoice XML data")
        .process("invoiceGenerator")
        .marshal(jaxbDataFormat)
        .to("file:{{xml.location}}");

我的发电机惨叫一声:

@Override
    public void process(Exchange exchange) throws Exception {

        Random random = new Random();
        List<Invoice> invoices = new LinkedList<>();

        for (int i = 0; i < 100; i++) {

            String invoiceNumber = String.format("invoice-%d", random.nextInt());

            Invoice invoice = new Invoice();
            invoice.setInvoiceNumber(invoiceNumber);
            invoice.setAmount(random.nextDouble());

            Instant now = Instant.now();

            GregorianCalendar cal1 = new GregorianCalendar();
            cal1.setTimeInMillis(now.toEpochMilli());
            invoice.setInsertionDate(DatatypeFactory.newInstance().newXMLGregorianCalendar());
            invoice.setInvoiceType(INVOICE_TYPE[random.nextInt(INVOICE_TYPE.length)]);
            invoices.add(invoice);
        }
        exchange.getMessage().setBody(invoices);
    }

我已经尝试实现了将java.util.LinkedList转换为所需类型的转换器:java.io.InputStream
与:

@Converter
    public InputStream ListToInputStream(List<Invoice> invoices) throws IOException {

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ByteArrayInputStream bios = null;
            try {

                ObjectFactory objFactory = new ObjectFactory();

                JAXBContext jaxbContext = JAXBContext.newInstance("com.webinage.model");
                Marshaller marshaller = jaxbContext.createMarshaller();
                marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

                for(Invoice invoice : invoices) {
                    marshaller.marshal(invoice, baos);
                }
                bios = new ByteArrayInputStream(new byte[baos.size()]);
                bios.read(baos.toByteArray());
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                baos.close();
            }
            return bios;
    }

但是同样的例外出现了...你有什么想法吗?

cgyqldqp

cgyqldqp1#

很可能List不是您希望输入到Exchange中的内容。我假设您希望输出XML如下所示:

<Invoices>
 <Invoice>...</Invoice>
 <Invoice>...</Invoice>
 ...
</Invoices>

在这种情况下,声明一个容器类可能是最简单的方法:

@XmlRootElement
public class Invoices {
  private List<Invoice> invoices;
}

(省略getter和setter)。
然后生成架构并将此Invoices对象放入Exchange中。

相关问题