本文介绍了java.lang.IllegalArgumentException:缺少某些字段(可选或强制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Apache Camel Bindy 创建一个固定文件阅读器,但出现异常.请帮我找到解决方案.没有页眉和页脚,效果很好.

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

文件内容:

101-08-200930A920A960A940A910A950A89000000002

新的例外:

java.lang.IllegalArgumentException:缺少某些字段(可选或必填),第 2 行在 org.apache.camel.dataformat.bindy.BindyFixedLengthFactory.bind(BindyFixedLengthFactory.java:295) ~[camel-bindy-2.19.1.jar:2.19.1]在 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 无法使用修饰符"访问 com.camel.examples.OrderHeader 类的成员在 org.apache.camel.util.ObjectHelper.newInstance(ObjectHelper.java:1686) ~[camel-core-2.19.1.jar:2.19.1]

路由器:

//@Override公共无效配置()抛出异常{比较器比较器 = 新比较器(){@覆盖公共 int 比较(订单 o1,订单 o2){返回 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)公共类订单{私有@Link OrderHeader 标头;私人@Link OrderFooter 页脚;@DataField(位置 = 1,长度 = 2)私人内部订单号;@DataField(位置 = 3,长度 = 2)私人字符串clientNr;@覆盖公共字符串 toString() {//TODO 自动生成的方法存根返回 orderNr + clientNr + 页眉 + 页脚;}公共 OrderHeader getHeader() {返回标题;}public void setHeader(OrderHeader header) {this.header = 标题;}公共 OrderFooter getFooter() {返回页脚;}公共无效setFooter(OrderFooter页脚){this.footer = 页脚;}公共 int getOrderNr() {退货单编号;}公共无效 setOrderNr(int orderNr) {this.orderNr = orderNr;}公共字符串 getClientNr() {返回客户编号;}public void setClientNr(String clientNr) {this.clientNr = clientNr;}}@FixedLengthRecord公共类 OrderFooter {@DataField(位置 = 1,长度 = 1)公共整数记录类型;@DataField(位置 = 2,长度 = 9)public String numberOfRecordsInTheFile;公共字符串 toString() {返回 recordType + ":" + numberOfRecordsInTheFile;}公共 int getRecordType() {返回记录类型;}public void setRecordType(int recordType) {this.recordType = 记录类型;}公共字符串 getNumberOfRecordsInTheFile() {返回 numberOfRecordsInTheFile;}public void setNumberOfRecordsInTheFile(String numberOfRecordsInTheFile) {this.numberOfRecordsInTheFile = numberOfRecordsInTheFile;}}@FixedLengthRecord公共类 OrderHeader {@DataField(位置 = 1,长度 = 1)公共 int recordType = 1;@DataField(pos = 2, length = 10, pattern = "dd-MM-yyyy")公共日期记录日期;公共字符串 toString() {return recordType + ":" + new SimpleDateFormat("dd-MM-yyyy").format(recordDate);}公共 int getRecordType() {返回记录类型;}public void setRecordType(int recordType) {this.recordType = 记录类型;}公共日期 getRecordDate() {返回记录日期;}public void setRecordDate(Date recordDate) {this.recordDate = 记录日期;}}

依赖关系:

<依赖><groupId>io.hawt</groupId><artifactId>hawtio-springboot</artifactId><version>1.5.2</version></依赖><依赖><groupId>io.hawt</groupId><artifactId>hawtio-core</artifactId><version>1.5.2</version></依赖></依赖项><属性><java.version>1.8</java.version></属性><构建><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></插件></plugins></build></项目>
解决方案

我快速浏览了骆驼绑定组件测试,但找不到将@FixedLengthRecord(header,footer) 与@Link 结合在一起的测试

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

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

    @FixedLengthRecord(length = 4, paddingChar = ' ', header = OrderHeader.class, footer = OrderFooter.class)公共类订单{

     私有 OrderHeader 头;私有 OrderFooter 页脚;}

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

    .unmarshal(bindy).split(body()).process(exchange -> {订单 order = exchange.getIn().getBody(Order.class);
    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单元测试这里

I am trying to create a fixedfile reader using Apache Camel Bindy but am getting exception.Please help me to find the solution.Without header and footer which is working well.

Update:File is small to understand now and public is added

File Content:

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

New Exception:

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]

Exception:

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]

Router:

// @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));

Model Java classes:

    @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;
        }
    }

Dependencies:

<?
解决方案

I had a quick look at the camel bindy component tests and I could not find one that combines @FixedLengthRecord(header,footer ) with @Link

One possible workaround to your issue could be to

  1. Remove @Link from your header and footer fields in Order e.g

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

        private OrderHeader header;
        private  OrderFooter footer;
    }
    

    2.Then the header and footer are successfully processed by bindy and added in the exchange as headers("CamelBindyFixedLengthHeader","CamelBindyFixedLengthFooter") so with a processor you can set them to the order header,footer fields afterwardse.g

    .unmarshal(bindy).split(body()).process(exchange -> {
    Order order = exchange.getIn().getBody(Order.class);
    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())); });

Related camel unit test here

这篇关于java.lang.IllegalArgumentException:缺少某些字段(可选或强制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:33