Fixefid is a Java library for working with flat fixed formatted text files and CSV files.
A lot of improvements has been carried out:
Validation: now it’s possibile to obtain the validation info of all fields. That’s simplify the error management. For instance, if something goes wrong during the processing of the input string, no exception is thrown, and it’s possibile to obtain the validation error info of all fields.
Annotation: all configurations can be managed with annotations. The extended properties style has been maintained but only for advanced configuration, like call back needs.
Valid values accepted list in field annotation
Enum style: it’s been deprecated and not longer supported
Using a REST Service for converting fixed fields text record to json with Fixedfid java library.
Converting fixed fields text record to JSON can be realized in many ways. A solution can be the using of a REST Service combined with the Fixefid java library.
The environment is as a follows:
Java 8
Spring Boot 2.3.4.RELEASE
Spring Web
Fixefid 1.1.0
Spring Doc Openapi 1.5.0
The Fixefid java library permits to define a fixed fields text record with Java Bean or Java Enum. In this case the definition by Java Bean can be used to annotate a resource representation class of a REST Service.
For instance, we want converting a customer record like this one:
String record = "0000000000000000001Paul Robinson ";
If the persistance layer is realized with JPA, we can mapping record fields directly to Entity fields
Fixefid is a java library wich permits to define a fixed fields text record with Java Bean or Java Enum. Often a text record must be retrieved from data persisted on a database. Or a text record must be persisted on a database.
The solution is to define a mapping from the record’s fields with the persistence model. If the persistance layer is realized with JPA Entities, the mapping can be done directly to the JPA Entity with the Fixefid annotations. Infact a JPA Entity is a POJO, that’s a Java Bean. And so, we can annotate the JPA Entity with the Fixefid annotations to realize the mapping, without the need to create two models, one for the record and another one for the JPA Entity.
The environment is as a follows:
Java 8
Spring Boot 2.3.4.RELEASE
Spring Data JPA
Fixefid 1.1.0
H2 Database
For example we can have a Customer bean like this one:
The Customer above is annotated with Entity and FixefidRecord. The fields are annotated with FixefidField and other JPA annotations. To obtain the record from the database:
Customer customer = repository.findById(1L);
String record = new BeanRecord(customer).toString();
To save the record to the database:
String newRecord = "0000000000000000001Paul Robinson ";
Customer newCustomer = new Customer();
new BeanRecord(newCustomer, newRecord);
repository.save(newCustomer);
I made a video tutorial where the example above is explained in the detail way.
Howto avoid substring approach to deal with fixed fields formatted text records. The solution is the Java library Fixefid
Often we have to develop Java methods to deal with fixed fields formatted text records. The format is often used nowadays yet to files transfer between mainframe and unix machines. Almost always the approach is using substring like this:
But that is an error prone pattern. The records are length thousands of chars with many many fields and make a mistake with the offset is not so rarely. And moreover when you have to format the fields to create the record, you have to deal with string right padding, zero lead numeric left padding, boolean format, date format, decimal format, decimal separator, etc etc…
The solution is using a Java library like Fixefid, wich permits to start from a specification in excel, model the record with Java Bean or Java Enum and deal with fields by getter, setter and toString Java methods.
I’ve made a video tutorial where I explain in the detail way the process to start from an excel like this:
to create a Java Bean model like this:
to obtain a records txt file like this:
In this way we can using the standard java bean development to deal with fixed fields formatted text files.
For example you can create a Java Bean like this:
@FixefidRecord(recordLen = 600)
public class Student extends Person {
@FixefidField(fieldLen = 10, fieldOrdinal = 8, fieldType = FieldType.N,
fieldMandatory = FieldMandatory.INOUT)
private Long studentId;
….
}