简体   繁体   English

beanio映射文件中的maxLength功能不起作用

[英]maxLength feature in beanio mapping file is not working

我有一个需要在其中将字段值写到最大长度为25的文件上的要求。从数据库中,我得到的长度为30。我在bean映射文件中使用了“ maxLength = 25”属性。长度大于25。有人可以建议任何解决方案或解决方法吗?

It is not possible to force BeanIO validations when writing (marshalling) objects to an OutputStream for delimited formats from my understanding of the documentation, according to Section 4.6.2 Field Validation . 根据第4.6.2节“字段验证” ,根据我对文档的理解,在将对象写入(编组)对象到OutputStream定界格式时,不可能强制进行BeanIO验证。 Emphasis mine. 强调我的。

4.6.2. 4.6.2。 Field Validation 现场验证

BeanIO supports several common field validation rules when reading an input stream. 读取输入流时,BeanIO支持几种常见的字段验证规则。 All field validation rules are validated against the field text before type conversion. 在类型转换之前,将针对字段文本验证所有字段验证规则。 When field trimming is enabled, trim="true", all validations are performed after the field's text has first been trimmed. 启用字段修剪功能,trim =“ true”时,将在第一次修剪字段的文本后执行所有验证。 Field validations are ignored when writing to an output stream. 写入输出流时,将忽略字段验证。

I guess it is working for fixed length formats otherwise you would have an invalid record (although the same argument probably applies to any format) 我猜想它适用于定长格式,否则您将获得无效的记录(尽管相同的参数可能适用于任何格式)

You have 2 options here: 您在这里有2个选项:

  1. Enforce the maximum length in your setter and/or getter methods if you want to be paranoid. 如果您想偏执,请在您的setter和/或getter方法中增加最大长度。 This will affect your entire application (although I think it is a good idea to ensure your data consistency). 这将影响您的整个应用程序(尽管我认为确保数据一致性是一个好主意)。 You could do this in various ways and with the help of libraries (Apache Commons Lang's StringUtils come to mind). 您可以通过各种方式并在库的帮助下完成此操作(想到Apache Commons Lang的StringUtils )。 I'm leaving out any null checks, which you should take care of when using these approaches. 我遗漏了所有null检查,使用这些方法时应注意这些检查。

     public String getFirstName() { return firstName.substring(0, 25); // this should really not be necessary if you use the setter } public void setFirstName(final String newFirstName) { firstName = newFirstName.substring(0, 25); } 

    This would not require any changes in your mapping.xml file. 这不需要对您的mapping.xml文件进行任何更改。

  2. Enforce the maximum length in an alternative setter and/or getter methods used explicitly by BeanIO for writing (marshalling) your data. 在BeanIO显式用于写入(编组)数据的替代setter和/或getter方法中增加最大长度。 Your getter/setter methods then don't do anything special and you add a new getter/setter pair per field. 然后,您的getter / setter方法不会做任何特殊的事情,并且您在每个字段中添加了一个新的getter / setter对。 You could use just a custom getter method when you only want to enforce the length when writing the data. 当您只想在写入数据时强制使用长度时,可以仅使用自定义getter方法。

     public String getFirstName() { return firstName; } public void setFirstName(final String newFirstName) { firstName = newFirstName; } // the BeanIO specific getter/setter public String getFirstNameBIO() { return firstName.substring(0, 25); } public void setFirstNameBIO(final String newFirstName) { firstName = newFirstName.substring(0, 25); } 

    This would also require changes in your mapping.xml file. 这还需要更改您的mapping.xml文件。

     <?xml version="1.0" encoding="UTF-8"?> <beanio xmlns="http://www.beanio.org/2012/03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd"> <stream name="DailyCasesSndStream" format="delimited" strict="true"> <parser> <property name="delimiter" value="|"/> </parser> <record name="DailyCasesRecord" class="com.run.daily.batch.util.DailyCasesRecord" minOccurs="0" maxOccurs="unbounded"> <field name="firstName" maxLength="25" getter="getFirstNameBIO" setter="setFirstNameBIO"/> <!-- OR if you only want the new getter to be used --> <field name="midName" maxLength="25" getter="getMidNameBIO"/> </record> </stream> </beanio> 

    Using this test code: 使用此测试代码:

     final DailyCasesRecord dailyCasesRecord = new DailyCasesRecord(); dailyCasesRecord.setFirstName("FirstNameFirstNameFirstName"); dailyCasesRecord.setMidNameBIO("MidNameMidNameMidNameMidName"); final StreamFactory factory = StreamFactory.newInstance(); factory.loadResource("mapping.xml"); BeanWriter beanWriter = null; try (final StringWriter out = new StringWriter()) { beanWriter = factory.createWriter("DailyCasesSndStream", out); beanWriter.write(dailyCasesRecord); System.out.println(out.toString()); } catch (final Exception e) { System.err.println(e.getMessage()); } finally { if (beanWriter != null) { beanWriter.close(); } } 

    I get this output: 我得到以下输出:

     FirstNameFirstNameFirstNa|MidNameMidNameMidNameMidN 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM