2016-04-27 45 views
2

在我的路線中,我試圖解組傳入的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

+0

JAXB生成的類應該有ObjectFactory。我沒有看到你的項目結構! –

+0

我已按照建議更新了圖像。包含片段之後,我仍然無法將其加入處理器。我已經通過建議更新了git倉庫中的代碼,以供參考。 https://github.com/navigator007/MyRepo.git –

+0

你碰巧再試一次。它工作!? –

回答

1

你應該使用類似下面的代碼片段

  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目錄結構。

enter image description here