@Test public void testMarshal() throws Exception { final DataFormat bindy = new BindyCsvDataFormat(Customer.class); CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .marshal(bindy); } }); camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); String result = producer.requestBody("direct:start", new Customer("John", "Doe"), String.class); Assert.assertEquals("John,Doe", result.trim()); } finally { camelctx.stop(); } }
@Test public void testUnmarshal() throws Exception { final DataFormat bindy = new BindyCsvDataFormat(Customer.class); CamelContext camelctx = new DefaultCamelContext(); camelctx.addRoutes(new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .unmarshal(bindy); } }); camelctx.start(); try { ProducerTemplate producer = camelctx.createProducerTemplate(); Customer result = producer.requestBody("direct:start", "John,Doe", Customer.class); Assert.assertEquals("John", result.getFirstName()); Assert.assertEquals("Doe", result.getLastName()); } finally { camelctx.stop(); } }
@Bean @ConditionalOnClass(CamelContext.class) @ConditionalOnMissingBean(BindyCsvDataFormat.class) public BindyCsvDataFormat configureBindyCsvDataFormat( CamelContext camelContext, BindyCsvDataFormatConfiguration configuration) throws Exception { BindyCsvDataFormat dataformat = new BindyCsvDataFormat(); if (dataformat instanceof CamelContextAware) { ((CamelContextAware) dataformat).setCamelContext(camelContext); } Map<String, Object> parameters = new HashMap<>(); IntrospectionSupport.getProperties(configuration, parameters, null, false); IntrospectionSupport.setProperties(camelContext, camelContext.getTypeConverter(), dataformat, parameters); return dataformat; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .marshal().bindy(BindyType.Csv, org.apache.camel.dataformat.bindy.csv2.WeatherModel.class) .to("direct:middle"); from("direct:middle") .unmarshal(new BindyCsvDataFormat(org.apache.camel.dataformat.bindy.csv2.WeatherModel.class)) .to("mock:result"); } }; }
@Override protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { @Override public void configure() throws Exception { from("direct:start") .unmarshal(new BindyCsvDataFormat(org.apache.camel.dataformat.bindy.csv2.WeatherModel.class)) .to("mock:result"); } }; }
@Override public void configure() throws Exception { final DataFormat bindy = new BindyCsvDataFormat(org.camelcookbook.transformation.csv.model.BookModel.class); final DataFormat jaxb = new JaxbDataFormat("org.camelcookbook.transformation.myschema"); final XmlJsonDataFormat xmlJsonFormat = new XmlJsonDataFormat(); xmlJsonFormat.setRootName("bookstore"); xmlJsonFormat.setElementName("book"); xmlJsonFormat.setExpandableProperties(Arrays.asList("author", "author")); from("direct:start") .choice() .when(header(Exchange.FILE_NAME).endsWith(".csv")) .unmarshal(bindy) .bean(MyNormalizer.class, "bookModelToJaxb") .to("mock:csv") .when(header(Exchange.FILE_NAME).endsWith(".json")) .unmarshal(xmlJsonFormat) .to("mock:json") .when(header(Exchange.FILE_NAME).endsWith(".xml")) .unmarshal(jaxb) .to("mock:xml") .otherwise() .to("mock:unknown") .stop() .end() .to("mock:normalized"); }
@Override public void configure() throws Exception { final DataFormat bindy = new BindyCsvDataFormat(org.camelcookbook.transformation.csv.model.BookModel.class); from("direct:unmarshal").unmarshal(bindy); from("direct:marshal").marshal(bindy); }