无法使用骆驼中的jaxb解组。不调用处理器
问题描述:
在我的路线中,我试图解组传入的XML消息。但是,当我执行代码时,它跳过执行.processor,我无法弄清楚实际的错误(因为它没有给出)。无法使用骆驼中的jaxb解组。不调用处理器
代码:
package nl.hari.local.cust;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.model.dataformat.JaxbDataFormat;
public class XMLtoObject_Camel {
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
JaxbDataFormat jaxbDataFormat = new JaxbDataFormat();
JAXBContext con = JAXBContext.newInstance(Address.class);
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("file://C:/Hari/TstFolder/"
+ "?noop=true" + "&autoCreate=false" + "&flatten=false" + "&delete=false"
+ "&bufferSize=128")
.unmarshal(jaxbDataFormat)
//Doesn't invoke processor - start
.process(new Processor(){
public void process(Exchange exchange) throws Exception {
Address add = (Address) exchange.getIn().getBody();
System.out.println("city is" + add.getCity());
}
});
//Doesn't invoke processor - End
}
});
}
}
这是我使用的架构。我用来测试的XML在Eclipse中生成,JAXB类也是如此。
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.hari.nl/Address"
xmlns:tns="http://www.cimt.nl/Address"
elementFormDefault="qualified">
<element name="address">
<complexType>
<sequence>
<element type="string" name="city"/>
<element type="string" name="country"/>
</sequence>
<attribute type="byte" name="id"/>
</complexType>
</element>
</schema>
下面的链接中包含的项目结构的屏幕抓取 http://i.stack.imgur.com/4IWhD.png
答
你应该使用类似下面的代码片段
ClassLoader cl = ObjectFactory.class.getClassLoader();
JAXBContext jc = JAXBContext.newInstance(SomeGeneratedClass.class.getPackage().getName(), cl);
JaxbDataFormat jaxb = new JaxbDataFormat(jc);
jaxb.setPartClass(SomeGeneratedClass.class.getName());
此外,解编将其转换为字符串之前。
.convertBodyTo(String.class)
.unmarshal(jaxb)
最终代码应如下所示。
package nl.hari.local.cust;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import org.apache.camel.ExchangePattern;
import org.apache.camel.LoggingLevel;
import org.apache.camel.ValidationException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.converter.jaxb.JaxbDataFormat;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.impl.DefaultCamelContext;
import nl.hari.local.jaxb.Address;
import nl.hari.local.jaxb.ObjectFactory;
public class Camel_JAXB_UnMarshal {
public static void main(String[] args) throws Exception {
CamelContext context = new DefaultCamelContext();
try {
ClassLoader cl = ObjectFactory.class.getClassLoader();
JAXBContext jc = JAXBContext.newInstance(Address.class.getPackage().getName(), cl);
final JaxbDataFormat jaxb = new JaxbDataFormat(jc);
jaxb.setPartClass(Address.class.getName());
context.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
from("file:resources"). //This can be changed as per ur requirement.
convertBodyTo(String.class)
.unmarshal(jaxb)
.process(new Processor() {
public void process(Exchange exchange) throws Exception {
Address add = (Address) exchange.getIn().getBody();
System.out.println("city is" + add.getCity());
}
});
}
});
context.start();
Thread.sleep(2000);
} finally {
context.stop();
}
}
}
和Eclipse目录结构。
JAXB生成的类应该有ObjectFactory。我没有看到你的项目结构! –
我已按照建议更新了图像。包含片段之后,我仍然无法将其加入处理器。我已经通过建议更新了git仓库中的代码,以供参考。 https://github.com/navigator007/MyRepo.git –
你碰巧再试一次。它工作!? –