0

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.

TechGeek
  • 480
  • 1
  • 8
  • 22
  • What is the stacktrace and message of the actual exception that you catch? Also the data that you send with the producer template hat more fields (7) than defined in Employee (4), maybe that is the cause? – SebastianBrandt Sep 14 '19 at 17:31
  • @SebastianBrandt It was returning the Exception, The conversion is not happening.I would like to know if I'm missing anything in that code. – TechGeek Sep 15 '19 at 04:20
  • Please add the line "e.printStackTrace();" to the catch block and post what it writes to the console. – SebastianBrandt Sep 15 '19 at 09:41

0 Answers0