I have beanio mappings file for delimeted file and required check is working but minLength is not working. Could you help with this? Geroge person record line is not showing minLength error for salary empty.
<record name="detail" minOccurs="0" maxOccurs="unbounded"
class="com.test.Person">
<field name="firstName"/>
<field name="surname"/>
<field name="salary" minLength="1"/>
<field name="age" required="true"/>
</record>
Input:
John|Smith|300000.00|34
Peter|William|800000.00|
Gregory|Rocky||30
Output:
[Person [firstName=John, surname=Smith, salary=300000.0, aget=34], Person [firstName=Gregory, surname=Rocky, salary=0.0, aget=30]]
Errors:
3 : age : [Required field not set]
Java Code: Update
public void readCSVFileUsingBeanIo() throws FileNotFoundException {
StreamFactory factory = StreamFactory.newInstance();
factory.loadResource("person.xml");
StringBuilder rejects = new StringBuilder();
// read it from the classpath : src/main/resources
InputStream in = new FileInputStream(new File(
"person.csv"));
BeanReader reader = factory.createReader("persons", new InputStreamReader(in));
reader.setErrorHandler(new BeanReaderErrorHandlerSupport() {
public void invalidRecord(InvalidRecordException ex) throws Exception {
for (int i = 0, j = ex.getRecordCount(); i < j; i++) {
final int ii = i;
if (ex.getRecordContext(i).hasErrors()) {
ex.getRecordContext(i).getFieldErrors().keySet().stream().forEach(key -> {
rejects.append("\n");
rejects.append(ex.getRecordContext(ii).getLineNumber() + " : ");
rejects.append(ex.getRecordContext(ii).getRecordText() + " Field(" + key + ") : Error "
+ ex.getRecordContext(ii).getFieldErrors(key));
});
rejects.append("\n");
}
}
}
});
Object record = null;
List<Person> persons = new ArrayList<Person>();
while ((record = reader.read()) != null) {
if ("header".equals(reader.getRecordName())) {
@SuppressWarnings("unchecked")
Map<String, Object> header = (Map<String, Object>) record;
System.out.println(header.get("fileDate"));
} else if ("detail".equals(reader.getRecordName())) {
Person person = (Person) record;
persons.add(person);
}
}
System.out.println(persons);
System.out.println("Errors:");
System.out.println(rejects);
}
Change age attribute to
required="false"or remove it.