יום שישי, 21 ביוני 2013

Jaxb marshaling in camel

 

The jaxb pojo (ignoe the csv annotation that will use as for later samples )

  1: package org.apache.camel.example.console;
  2: 
  3: import javax.xml.bind.annotation.XmlAccessorType;
  4: import javax.xml.bind.annotation.XmlAttribute;
  5: import javax.xml.bind.annotation.XmlRootElement;
  6: 
  7: import org.apache.camel.dataformat.bindy.annotation.CsvRecord;
  8: import org.apache.camel.dataformat.bindy.annotation.DataField;
  9: 
 10: @CsvRecord(separator = ",", crlf = "UNIX")
 11: @XmlRootElement
 12: public class PersoneData {
 13:   @XmlAttribute
 14:   @DataField(pos = 1)
 15:   public String Name;
 16:   
 17:   @DataField(pos = 2)
 18:   @XmlAttribute
 19:   public int Age;
 20:   
 21:   @DataField(pos = 3)
 22:   @XmlAttribute
 23:   public String Email;
 24:   
 25:   @DataField(pos = 4)
 26:   @XmlAttribute
 27:   public String Address;
 28: }

The test program:


  1: package org.apache.camel.example.console;
  2: import java.util.ArrayList;
  3: import java.util.List;
  4: import java.util.Map;
  5: 
  6: import org.apache.camel.CamelContext;
  7: import org.apache.camel.Exchange;
  8: import org.apache.camel.Predicate;
  9: import org.apache.camel.ProducerTemplate;
 10: import org.apache.camel.builder.RouteBuilder;
 11: import org.apache.camel.dataformat.bindy.csv.BindyCsvDataFormat;
 12: import org.apache.camel.impl.DefaultCamelContext;
 13: import org.apache.camel.model.dataformat.BindyType;
 14: import org.apache.camel.model.language.ELExpression;
 15: import org.apache.camel.spi.DataFormat;
 16: 
 17: 
 18: public class TestCodeForBlog {
 19:   
 20:   public static RouteBuilder createRoute() {
 21:     
 22:     final DataFormat bindy = new BindyCsvDataFormat("org.apache.camel.example.console");
 23:         
 24:     return new RouteBuilder() {
 25:     public void configure() throws Exception {
 26:     from("direct:toXML")
 27:     .marshal().jaxb("org.apache.camel.example.console").  
 28:     setHeader(Exchange.FILE_NAME, constant("test.Csv"))
 29:         .to("file:target/TestmyFiles").
 30:       to("stream:out");
 31:     }
 32:     };
 33:     }
 34:   
 35:   public static void main(String[] args) {
 36:     
 37:     try {
 38:       CamelContext context = new DefaultCamelContext();  
 39:     
 40:       context.addRoutes(createRoute());
 41:       context.start();
 42:     
 43:       PersoneData thePersoneData = new PersoneData();
 44:       
 45:       thePersoneData.Name = "Zvika";
 46:       
 47:       thePersoneData.Age = 42;
 48:       
 49:       thePersoneData.Email = "Loveme42@hotmail.com";
 50:       
 51:       thePersoneData.Address ="Bilo 9b Ness-Ziona";
 52:             
 53:       Thread.sleep(1000);  
 54:       
 55:       ProducerTemplate template = context.createProducerTemplate();
 56:       
 57:       template.sendBody("direct:toXML", thePersoneData);
 58:       
 59:       //Let the camel thread route to work in its thread
 60:       Thread.sleep(1000);
 61:        System.out.print("I'm out");
 62:        
 63:     } catch (Exception e) {
 64:       // TODO Auto-generated catch block
 65:       e.printStackTrace();
 66:     }
 67:   }
 68: }
 69: 

 


 Solving jaxb no index problem:
In case while executing the program a problem that no jaxb index appears create a simple jaxb.index file and add it in the pojo file location


Capture38


The jaxb.index file contains only the following string :PersoneData


 


And the Result


  1: <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2: <personeData name="Zvika" age="42" email="Loveme42@hotmail.com" address="Bilo 9b Ness-Ziona"/>
  3: I'm out

http://stackoverflow.com/questions/6017146/jaxbrepresentation-gives-error-doesnt-contain-objectfactory-class-or-jaxb-index

אין תגובות:

הוסף רשומת תגובה