简体   繁体   中英

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 . Emphasis mine.

4.6.2. Field Validation

BeanIO supports several common field validation rules when reading an input stream. 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. 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:

  1. Enforce the maximum length in your setter and/or getter methods if you want to be paranoid. 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). I'm leaving out any null checks, which you should take care of when using these approaches.

     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.

  2. Enforce the maximum length in an alternative setter and/or getter methods used explicitly by BeanIO for writing (marshalling) your data. Your getter/setter methods then don't do anything special and you add a new getter/setter pair per field. You could use just a custom getter method when you only want to enforce the length when writing the data.

     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.

     <?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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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