TechGeek
TechGeek

Reputation: 508

Exception while transforming Csv to Pojo using apache camel 2.24.1

I'm trying to convert Csv data to Pojo using Apache Camel 2.24.1, but it is throwing exception.Can anyone help me to solve this issue.

CsvToPojoRoute.java

    public class CsvToPojoRoute extends RouteBuilder {    
    public void configure() throws Exception {

    DataFormat bindy = new BindyCsvDataFormat(com.tiger.puli.dto.Employee.class);

    from("direct:start")
            .unmarshal(bindy)
            .process(new Processor() {
                @Override
                public void process(Exchange exchange) throws Exception {
                    Message in = exchange.getIn();
                    List<Map<String,Object>> modelMap =
                            (List<Map<String,Object>>) in.getBody();

                    Employee emp = (Employee) modelMap.get(0).get(Employee.class.getCanonicalName());

                    System.out.println("EmployeeName:"+emp.getFirstName()+" "+emp.getLastName());
                }
            }).end();
}

Employee.java

@CsvRecord(separator = ",")
public class Employee implements Serializable {

  @DataField(pos = 1,trim = true)
  private String firstName;

  @DataField(pos = 2)
  private String lastName;

  @DataField(pos = 3)
  private String dept;

  @DataField(pos = 4,trim = true)
  private String fullTime;

  }

Main.java

  public static void main(String[] args) {

    ApplicationContext springContext = new ClassPathXmlApplicationContext("classpath:camel-context.xml");

    SpringCamelContext camelContext = springContext.getBean("camelcontextbean",SpringCamelContext.class);

    try{
        camelContext.start();
        System.out.println("Started");

    camelContext.addRoutes(springContext.getBean("CsvToPojoRoute", RoutesBuilder.class));

        ProducerTemplate template = camelContext.createProducerTemplate();
        System.out.println("ProducerTemplate");
        template.sendBody("direct:start",
                "john,doe,d1,true,city1,state1,1234");

    }catch (Exception e){
        System.out.println("Exception occured while biding csv to pojo:"+e.getMessage());
    }

  }

camel-context.xml

  <?xml version="1.0" encoding="UTF-8"?>
  <beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">

  <camelContext lazyLoadTypeConverters="true" autoStartup="false" id="camelcontextbean" xmlns="http://camel.apache.org/schema/spring"/>

  <bean id="CsvToPojoRoute" class="com.tiger.puli.config.CsvToPojoRoute" />
   </beans>

It is returning "Exception occured while biding csv to pojo". Can I know what's the issue.

Upvotes: 0

Views: 235

Answers (0)

Related Questions