יום שבת, 15 ביוני 2013

Dic to Json using Camel

The following camel example marshal an object into Json format .
The pom

  1: <?xml version="1.0" encoding="UTF-8"?>
  2: 
  3: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  4:  <modelVersion>4.0.0</modelVersion>
  5: 
  6:   <parent>
  7:     <groupId>org.apache.camel</groupId>
  8:     <artifactId>examples</artifactId>
  9:     <version>2.10.4</version>
 10:     <relativePath>..</relativePath>
 11:   </parent>
 12: 
 13:   <artifactId>camel-example-console</artifactId>
 14:   <packaging>jar</packaging>
 15:   <name>Camel :: Example :: Console</name>
 16:   <description>An example that reads input from the console</description>
 17: 
 18:   <dependencies>
 19: 
 20:     <dependency>
 21:       <groupId>org.apache.camel</groupId>
 22:       <artifactId>camel-core</artifactId>
 23:     </dependency>
 24:     <dependency>
 25:       <groupId>org.apache.camel</groupId>
 26:       <artifactId>camel-spring</artifactId>
 27:     </dependency>
 28:     <dependency>
 29:       <groupId>org.apache.camel</groupId>
 30:       <artifactId>camel-stream</artifactId>
 31:     </dependency>
 32: 
 33:     <!-- logging to the console -->
 34:     <dependency>
 35:       <groupId>org.slf4j</groupId>
 36:       <artifactId>slf4j-log4j12</artifactId>
 37:     </dependency>
 38:     <dependency>
 39:       <groupId>log4j</groupId>
 40:       <artifactId>log4j</artifactId>
 41:     </dependency>
 42:     <dependency>
 43:         <groupId>org.apache.camel</groupId>
 44:         <artifactId>camel-bindy</artifactId>
 45:         <version>2.0-M1</version>
 46:       </dependency>
 47:       <dependency>
 48:             <groupId>org.apache.camel</groupId>
 49:             <artifactId>camel-jaxb</artifactId>
 50:         </dependency>
 51:         <dependency>
 52:   <groupId>org.apache.camel</groupId>
 53:   <artifactId>camel-gson</artifactId>
 54:   <version>2.10.0</version>
 55: </dependency>
 56: <dependency>
 57:   <groupId>org.apache.camel</groupId>
 58:   <artifactId>camel-jackson</artifactId>
 59:   <version>2.9.2</version>
 60: </dependency>
 61: <dependency>
 62:   <groupId>org.apache.camel</groupId>
 63:   <artifactId>camel-xstream</artifactId>
 64:   <version>2.9.2</version>
 65: </dependency>
 66:     
 67:   </dependencies>
 68: 
 69:   <build>
 70:     <plugins>
 71:       <!-- Allows the example to be run via 'mvn compile exec:java' -->
 72:       <plugin>
 73:         <groupId>org.codehaus.mojo</groupId>
 74:         <artifactId>exec-maven-plugin</artifactId>
 75:         <configuration>
 76:           <mainClass>org.apache.camel.example.console.CamelConsoleMain</mainClass>
 77:           <includePluginDependencies>false</includePluginDependencies>
 78:         </configuration>
 79:       </plugin>
 80:     </plugins>
 81: 
 82:   </build>
 83: 
 84: </project>

The important dependencies for our example are :camel-gson,camel-jackson and camel-xstream.
The test code :


  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: 
 14: import org.apache.camel.spi.DataFormat;
 15: 
 16: public class TestCodeForBlog {
 17:   
 18:   public static RouteBuilder createRoute() {
 19:       
 20:     return new RouteBuilder() {
 21:     public void configure() throws Exception {
 22:     from("direct:toJson")
 23:     .marshal().json().
 24:     setHeader(Exchange.FILE_NAME, constant("test.Json"))
 25:         .to("file:target/TestmyFiles").
 26:       to("stream:out");
 27:     }
 28:     };
 29:     }
 30:   
 31:   public static void main(String[] args) {
 32:   
 33: 
 34:     try {
 35:       CamelContext context = new DefaultCamelContext();  
 36:     
 37:       context.addRoutes(createRoute());
 38:       context.start();
 39:       
 40:       Map<String,Object> theItem = new java.util.HashMap<String,Object>();
 41:       
 42:       theItem.put("Name", "Zvika");
 43:       theItem.put("Age", "42");
 44:       theItem.put("Email", "Loveme42@hotmail.com");
 45:       theItem.put("Address", "Bilo 9b Ness-Ziona");
 46:       
 47:       personDetails.add(theItem);
 48:     
 49:         ProducerTemplate template = context.createProducerTemplate();
 50:       template.sendBody("direct:toJson", theItem);
 51:     
 52:       //Let the camel thread route to work in its thread
 53:       Thread.sleep(1000);
 54:        System.out.print("I'm out");
 55:        
 56:     } catch (Exception e) {
 57:       // TODO Auto-generated catch block
 58:       e.printStackTrace();
 59:     }
 60:   }
 61: }

And the Result:
{"map":{"entry":[{"string":["Name","Zvika"]},{"string":["Age",42]},{"string":["Email","Loveme42@hotmail.com"]},{"string":["Address","Bilo 9b Ness-Ziona"]}]}}


I'm out

אין תגובות:

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