2016-04-27 8 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

関連する問題