Apache Camel 没有可用的类型转换器
Apache Camel No type converter available
我用 camel 定义了一个处理器,它允许我生成一个带有计时器的 jaxb java bean,并将 pojo 写入 xml 文件。
但是当我启动应用程序时出现以下错误:
08:09:00 警告 [or.ap.ca.co.ti.TimerConsumer](Camel(camel-1)线程 #2 - timer://generateInvoice)处理交换时出错。交换 [20E715FDB7EFE19-0000000000000000]。由以下原因引起:[java.io.IOException - org.apache.camel.NoTypeConversionAvailableException:没有类型转换器可用于从类型:java.util.LinkedList 转换为所需类型:java.io.InputStream,值为 [com.mycompany.model.Invoice@21e27bf , com.mycompany.model.Invoice@1fd7b4bf, com.mycompany.model.Invoice@9cb7bae, com.mycompany.model.Invoice@2751bc51 ...
我的代码如下:
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;
}
但是同样的异常出现了...
请问你有什么想法吗?
很可能 List 不是您要放入 Exchange 的内容。我假设您希望输出 XML 类似于:
<Invoices>
<Invoice>...</Invoice>
<Invoice>...</Invoice>
...
</Invoices>
在这种情况下,声明容器可能是最直接的 class:
@XmlRootElement
public class Invoices {
private List<Invoice> invoices;
}
(省略getter和setter)。
然后生成您的架构并将此 Invoices
对象放入 Exchange。
我用 camel 定义了一个处理器,它允许我生成一个带有计时器的 jaxb java bean,并将 pojo 写入 xml 文件。 但是当我启动应用程序时出现以下错误:
08:09:00 警告 [or.ap.ca.co.ti.TimerConsumer](Camel(camel-1)线程 #2 - timer://generateInvoice)处理交换时出错。交换 [20E715FDB7EFE19-0000000000000000]。由以下原因引起:[java.io.IOException - org.apache.camel.NoTypeConversionAvailableException:没有类型转换器可用于从类型:java.util.LinkedList 转换为所需类型:java.io.InputStream,值为 [com.mycompany.model.Invoice@21e27bf , com.mycompany.model.Invoice@1fd7b4bf, com.mycompany.model.Invoice@9cb7bae, com.mycompany.model.Invoice@2751bc51 ...
我的代码如下:
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;
}
但是同样的异常出现了... 请问你有什么想法吗?
很可能 List 不是您要放入 Exchange 的内容。我假设您希望输出 XML 类似于:
<Invoices>
<Invoice>...</Invoice>
<Invoice>...</Invoice>
...
</Invoices>
在这种情况下,声明容器可能是最直接的 class:
@XmlRootElement
public class Invoices {
private List<Invoice> invoices;
}
(省略getter和setter)。
然后生成您的架构并将此 Invoices
对象放入 Exchange。