java.lang.IllegalArgumentException:缺少某些字段(可选或必填)

java.lang.IllegalArgumentException: Some fields are missing (optional or mandatory)

我正在尝试使用 Apache Camel Bindy 创建一个固定文件 reader,但正在 exception.Please 帮我找到工作正常的 solution.Without 页眉和页脚。

更新: 文件太小了现在看不懂,添加了public

文件内容:

101-08-2009
30A9
20A9
60A9
40A9
10A9
50A8
9000000002

新异常:

java.lang.IllegalArgumentException: Some fields are missing (optional or mandatory), line: 2
    at org.apache.camel.dataformat.bindy.BindyFixedLengthFactory.bind(BindyFixedLengthFactory.java:295) ~[camel-bindy-2.19.1.jar:2.19.1]
    at org.apache.camel.dataformat.bindy.fixed.BindyFixedLengthDataFormat.createModel(BindyFixedLengthDataFormat.java:294) ~[camel-bindy-2.19.1.jar:2.19.1]

异常:

org.apache.camel.RuntimeCamelException: java.lang.IllegalAccessException: Class org.apache.camel.util.ObjectHelper can not access a member of class com.camel.examples.OrderHeader with modifiers ""
    at org.apache.camel.util.ObjectHelper.newInstance(ObjectHelper.java:1686) ~[camel-core-2.19.1.jar:2.19.1]

路由器:

// @Override
    public void configure() throws Exception {
        Comparator<Order> comparator = new Comparator<Order>() {
            @Override
            public int compare(Order o1, Order o2) {
                return o1.getOrderNr() - o2.getOrderNr();
            }
        };

       DataFormat bindy = new BindyFixedLengthDataFormat(Order.class);
        from("file:C:/Users/workspace/SampleProjects/in?delete=true")
                .routeId("ValidateFile-Post2Q")

                .unmarshal(bindy).split(body().sort(comparator));

型号Java类:

    @FixedLengthRecord(length = 4, paddingChar = ' ', header = OrderHeader.class, footer = OrderFooter.class)
public class Order {

    private @Link OrderHeader header;
    private @Link OrderFooter footer;

    @DataField(pos = 1, length = 2)
    private int orderNr;

    @DataField(pos = 3, length = 2)
    private String clientNr;


    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return orderNr + clientNr + header + footer;
    }

    public OrderHeader getHeader() {
        return header;
    }


    public void setHeader(OrderHeader header) {
        this.header = header;
    }


    public OrderFooter getFooter() {
        return footer;
    }


    public void setFooter(OrderFooter footer) {
        this.footer = footer;
    }

    public int getOrderNr() {
        return orderNr;
    }

    public void setOrderNr(int orderNr) {
        this.orderNr = orderNr;
    }

    public String getClientNr() {
        return clientNr;
    }

    public void setClientNr(String clientNr) {
        this.clientNr = clientNr;
    }
}

    @FixedLengthRecord
    public class OrderFooter {

        @DataField(pos = 1, length = 1)
        public int recordType ;

        @DataField(pos = 2, length = 9)
        public String numberOfRecordsInTheFile;

        public String toString() {
            return recordType + ":" + numberOfRecordsInTheFile;
        }

        public int getRecordType() {
            return recordType;
        }

        public void setRecordType(int recordType) {
            this.recordType = recordType;
        }

        public String getNumberOfRecordsInTheFile() {
            return numberOfRecordsInTheFile;
        }

        public void setNumberOfRecordsInTheFile(String numberOfRecordsInTheFile) {
            this.numberOfRecordsInTheFile = numberOfRecordsInTheFile;
        }   
    }

    @FixedLengthRecord
    public class OrderHeader {
        @DataField(pos = 1, length = 1)
        public int recordType = 1;

        @DataField(pos = 2, length = 10, pattern = "dd-MM-yyyy")
        public Date recordDate;

        public String toString() {
            return recordType + ":" + new SimpleDateFormat("dd-MM-yyyy").format(recordDate);
        }

        public int getRecordType() {
            return recordType;
        }

        public void setRecordType(int recordType) {
            this.recordType = recordType;
        }

        public Date getRecordDate() {
            return recordDate;
        }

        public void setRecordDate(Date recordDate) {
            this.recordDate = recordDate;
        }
    }

依赖关系:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.springframework</groupId>
    <artifactId>gs-spring-boot</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-spring-boot-starter</artifactId>
            <version>2.19.1</version>
        </dependency>

        <!-- Camel xquery support -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-saxon</artifactId>
            <version>2.19.1</version>
        </dependency>

        <!-- Camel JMS support and ActiveMq -->
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-jms</artifactId>
            <version>2.19.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-camel</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.activemq</groupId>
                    <artifactId>activemq-broker</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-ftp</artifactId>
            <version>2.19.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-bindy</artifactId>
            <version>2.19.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-stream</artifactId>
            <version>2.19.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-restlet</artifactId>
            <version>2.19.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-flatpack</artifactId>
            <version>2.19.1</version>
        </dependency>
        <!-- Hawtio dependency - a lightweight web console to monitor and manage 
            application -->
        <dependency>
            <groupId>io.hawt</groupId>
            <artifactId>hawtio-springboot</artifactId>
            <version>1.5.2</version>
        </dependency>
        <dependency>
            <groupId>io.hawt</groupId>
            <artifactId>hawtio-core</artifactId>
            <version>1.5.2</version>
        </dependency>



    </dependencies>

    <properties>
        <java.version>1.8</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

确保您的 POJO 是 public classes。它不是您的 POJO class 实例化另一个 class,它的 camel-bindy / camel-core 并且该代码在 org.apache.camel 包中运行,并且不能依赖包对 POJO 的可见访问。所以最重要的是,保持它 public 所以它的标准 Java 豆子。

camel-bindy 有时会尝试绑定到错误的模型对象。

我在网上找到的一个解决方案是尝试将每个模型对象放在不同的包中。(不是一个干净的解决方案,但足以知道问题出在哪里 - 我们的代码或库)

我遇到了类似的异常,虽然这个解决方案对我不起作用,但也许你可以试一试。

您可以在此处查看详细信息: Camel-bindy exception

我快速浏览了 camel bindy 组件测试,但找不到将 @FixedLengthRecord(header,footer) 与 @Link

组合在一起的测试

您的问题的一种可能解决方法是

  1. 从订单中的页眉和页脚字段中删除@Link,例如

    
    
     @FixedLengthRecord(length = 4, paddingChar = ' ', header = OrderHeader.class, footer = OrderFooter.class)
        public class Order {
    
    <pre><code>    private OrderHeader header;
        private  OrderFooter footer;
    } 
    

    2.Then 页眉和页脚由 bindy 成功处理并作为页眉添加到交换中("CamelBindyFixedLengthHeader","CamelBindyFixedLengthFooter")因此使用处理器您可以将它们设置为订单页眉,页脚字段之后 例如

    .unmarshal(bindy).split(body()).process(exchange -> { 
    Order order = exchange.getIn().getBody(Order.class);<br>
                     order.setFooter((OrderFooter)exchange.getIn().getHeader(BindyFixedLengthDataFormat.CAMEL_BINDY_FIXED_LENGTH_FOOTER,Map.class).get(OrderFooter.class.getName())); 
                       order.setHeader((OrderHeader)exchange.getIn().getHeader(BindyFixedLengthDataFormat.CAMEL_BINDY_FIXED_LENGTH_HEADER,Map.class).get(OrderHeader.class.getName()));
                });
    

相关camel单元测试here