0

我正在尝试使用 Apache Camel 2.24.1 将 Csv 数据转换为 Pojo,但它抛出异常。谁能帮我解决这个问题。

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();
}

雇员.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;

  }

主.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());
    }

  }

骆驼上下文.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>

它正在返回“将 csv 卖给 pojo 时发生异常”。我能知道是什么问题吗。

4

0 回答 0