我是Bean-IO的新手,当文件中有特定记录类型时,我试图为组的发生配置验证逻辑。例如,如果平面文件中有三个记录,如下所示。
560866
670972
57086659
我正在尝试设置以下逻辑
我成功使用@record批注中的minOccurs属性创建了第一个验证,但使用组无法对56&67执行相同的操作。
请在下面找到示例代码设置。
HeaderRecord类保存56&67记录详细信息
@Group
public class HeaderRecord {
@Record(minOccurs = 1)
public TX56 tx56;
@Record(minOccurs = 1)
public TX67 tx67;
}
RecordObject用于保存标题和订单项
public class RecordObject {
@Group(collection = List.class, minOccurs = 1)
List<HeaderRecord> headerRecords;
@Record(collection = List.class)
List<TX57> tx57s;
}
@Record(maxLength = 10, name = "TX56")
public class TX56 {
@Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "56", trim = true)
protected int id;
@Field(ordinal = 1, at = 2, length = 4, trim = true)
protected int number;
}
@Record(maxLength = 31, name = "TX67")
public class TX67 {
@Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "67", trim = true)
protected int id;
@Field(ordinal = 1, at = 2, length = 4, trim = true)
protected int number;
}
@Record(maxLength = 71, name = "TX57")
public class TX57 {
@Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "57", trim = true)
protected int id;
@Field(ordinal = 1, at = 2, length = 4, trim = true)
protected int number;
}
使用上述配置,当我尝试使用下面给出的记录来解析文件时,它会抛出UnexpectedRecordException。
560866
670972
57086659
堆栈跟踪:
2018-07-17 15:22:07,778 [http-nio-8080-exec-2]错误 org.apache.catalina.core.ContainerBase。[Tomcat]。[localhost]。[/]。[dispatcherServlet] -Servlet.service()用于 带有路径[]的上下文中的servlet [dispatcherServlet] 异常[请求处理失败;嵌套异常为 org.beanio.UnexpectedRecordException:已到达流的末尾,预期 记录“ tx56”]的根本原因是org.beanio.UnexpectedRecordException:End 已达到的流,预期记录为“ tx56” org.beanio.internal.parser.UnmarshallingContext.newUnsatisfiedRecordException(UnmarshallingContext.java:367)〜[beanio-2.1.0.jar:2.1.0] 在 org.beanio.internal.parser.Group.unmarshal(Group.java:127)〜[beanio-.1.0.jar:2.1.0] 在 org.beanio.internal.parser.DelegatingParser.unmarshal(DelegatingParser.java:39)〜[beanio-2.1.0.jar:2.1.0] 在 org.beanio.internal.parser.RecordCollection.unmarshal(RecordCollection.java:42)〜[beanio-2.1.0.jar:2.1.0] 在 org.beanio.internal.parser.Group.unmarshal(Group.java:118)〜[beanio-2.1.0.jar:2.1.0] 在 org.beanio.internal.parser.BeanReaderImpl.internalRead(BeanReaderImpl.java:106)〜[beanio-2.1.0.jar:2.1.0] 在 org.beanio.internal.parser.BeanReaderImpl.read(BeanReaderImpl.java:67)〜[beanio-2.1.0.jar:2.1.0] 在 dk.coop.integration.fileconversion.service.sampleapplication.createFixedLengthFile(sampleapplication.java:32)〜[classes / :?]
注意: 通过上述配置,以下情况可以正常工作
其他详细信息: 样本平面文件
560866
670972
560866
670972
560866
670972
57086659
57086659
57086659
57086659
52022
560866
670972
57086659
如上所示,在平面文件中,可能有多个标头记录和TX57记录可以作为单个实体出现。另外,可能还会有其他类型的记录,在这种情况下,我必须将第二次出现的TX56、67和57视为不同的项目。
在上面的示例中,前10个记录将形成单个recordObject,然后这些记录的第二次出现将形成第二个记录对象。抱歉,由于您之前没有共享,但是还有另一个包装器类,其中包含一个recordObject列表。
我在下面提供了正在运行的Maven项目Github URL。 https://github.com/Jayadeep2308/FlatFileParser
答案 0 :(得分:0)
编辑:在所有要求之后于2018年8月5日更新
我已经在类中创建了所有字段private
,并假定您已经安装了getters + setters。
我尝试了@Group
和@Record
注释的各种设置组合,因此下面的代码可能不是最佳的,但应该可以使用。
首先存储所有数据的主要组(WrapperObject
)
@Group(minOccurs = 1, maxOccurs = 1)
public class WrapperObject {
@Group(minOccurs = 0, maxOccurs = 1, collection = List.class)
private List<RecordObject> recordObjectList;
@Record(minOccurs = 0, maxOccurs = -1, collection = List.class)
private List<TX52> tx52s;
}
编辑:RecordObject
已更新为保存HeaderRecord
的列表,还更改了@Group
的值。
@Group(minOccurs = 0, maxOccurs = -1)
public class RecordObject {
@Group(minOccurs = 0, maxOccurs = -1, collection = List.class)
private List<HeaderRecord> headerRecords;
@Record(minOccurs = 0, maxOccurs = -1, collection = List.class)
private List<TX57> tx57s;
}
@Group(minOccurs = 0, maxOccurs = -1)
public class HeaderRecord {
@Record(minOccurs = 1, maxOccurs = 1)
private TX56 tx56;
@Record(minOccurs = 1, maxOccurs = 1)
private TX67 tx67;
}
在各个TX记录上,我在required=true
批注中为您的记录标识符字段添加了@Field
属性。
编辑:添加了TX52
@Record(maxLength = 74, name = "TX52")
public class TX52 {
@Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "52", trim = true, required = true)
private int id;
@Field(ordinal = 1, at = 2, length = 3, trim = true)
private int number;
}
@Record(maxLength = 10, name = "TX56")
public class TX56 {
@Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "56", trim = true, required = true)
private int id;
@Field(ordinal = 1, at = 2, length = 4, trim = true, required = true)
private int number;
}
@Record(maxLength = 31, name = "TX67")
public class TX67 {
@Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "67", trim = true, required = true)
private int id;
@Field(ordinal = 1, at = 2, length = 4, trim = true)
private int number;
}
@Record(maxLength = 71, name = "TX57")
public class TX57 {
@Field(ordinal = 0, at = 0, length = 2, rid = true, literal = "57", trim = true, required = true)
private int id;
@Field(ordinal = 1, at = 2, length = 4, trim = true)
private int number;
}
最后,我的测试代码:( EDIT :更新了测试数据)
@Test
public void test() {
final StreamFactory factory = StreamFactory.newInstance();
final StreamBuilder builder = new StreamBuilder("Jaydeep23")
.format("fixedlength")
.parser(new FixedLengthParserBuilder())
.addGroup(WrapperObject.class);
factory.define(builder);
final String scenario1 = "560866\n670972\n560866\n670972\n560866\n670972";
final String scenario2 = "560866\n670972\n560866\n670972\n560866\n670972\n57086659\n57086659\n57086659\n" +
"57086659\n560866\n670972\n57086659\n560866\n670972";
// invalid
final String scenario3 = "57086659\n57086659\n57086659\n57086659\n57086659";
final String scenario4 = "52022\n52066\n52054\n52120";
final String scenario5 = scenario1;
final String scenario6 = "560866\n670972\n560866\n670972\n560866\n670972\n57086659\n57086659\n57086659\n" +
"57086659\n52021\n52022\n52023\n560866\n670972\n57086659\n52023";
final String message = scenario1;
BeanReader beanReader = null;
Object object = null;
try (final Reader in = new BufferedReader(new StringReader(message))) {
beanReader = factory.createReader("Jaydeep23", in);
beanReader.setErrorHandler(new LoggingBeanReaderErrorHandler());
while ((object = beanReader.read()) != null) {
System.out.println("Object = " + object);
}
} catch (final Exception e) {
fail(e.getMessage());
} finally {
if (beanReader != null) {
beanReader.close();
}
}
}
生成以下输出:(编辑:使用您的toString()
方法)
Scenario 1 = [[Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
]null]null
Scenario 2 = [[Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
][Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
]]null
Scenario 3 - gives this error (which is correct according as TX57 is not allowed on its own:
Expected record/group 'tx56' at line 6
Scenario 4 = null[Record Type = 52, Store Number = 22
, Record Type = 52, Store Number = 66
, Record Type = 52, Store Number = 54
, Record Type = 52, Store Number = 120
]
Scenario 5 = [[Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
]null]null
Scenario 6 = [[Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
, Record Type = 56, Store Number = 866
Record Type = 67, Store Number = 972
][Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
, Record Type = 57, Store Number = 866
]][Record Type = 52, Store Number = 21
, Record Type = 52, Store Number = 22
, Record Type = 52, Store Number = 23
]
希望这会有所帮助。
让我知道它是否现在对您有用。