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 ";
to a json object like this one:
{ "id": 1, "firstName": "Paul", "lastName": "Robinson" }
To model the customer representation, we can create a resource representation class:
@FixefidRecord public class Customer { @FixefidField(fieldLen = 19, fieldOrdinal = 0, fieldType = FieldType.N) private Long id; @FixefidField(fieldLen = 50, fieldOrdinal = 1, fieldType = FieldType.AN) private String firstName; @FixefidField(fieldLen = 50, fieldOrdinal = 2, fieldType = FieldType.AN) private String lastName; protected Customer() { } public Customer(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } @Override public String toString() { return String.format("Customer[id=%d, firstName='%s', lastName='%s']", id, firstName, lastName); } public Long getId() { return id; } public String getFirstName() { return firstName; } public String getLastName() { return lastName; } }
The resource representation class is annotated with the Fixefid annotations. Then we can create the record request:
public class RecordRequest { private Long requestId; private String record; public String getRecord() { return record; } public void setRecord(String record) { this.record = record; } public Long getRequestId() { return requestId; } public void setRequestId(Long requestId) { this.requestId = requestId; } }
and the Customer response:
public class CustomerResponse { private Long requestId; private Long responseId; private Customer customer; public CustomerResponse(Long requestId, Long responseId, Customer customer) { this.requestId = requestId; this.responseId = responseId; this.customer = customer; } public Long getRequestId() { return requestId; } public void setRequestId(Long requestId) { this.requestId = requestId; } public Long getResponseId() { return responseId; } public void setResponseId(Long responseId) { this.responseId = responseId; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } }
last, the rest controller:
@RestController public class CustomerController { private final AtomicLong counter = new AtomicLong(); @PostMapping(path = "/recordtocustomer", consumes = "application/json", produces = "application/json") public CustomerResponse recordToCustomer(@RequestBody RecordRequest request) { Customer customer = new Customer(null, null); new BeanRecord(customer, request.getRecord()); return new CustomerResponse(request.getRequestId(), counter.incrementAndGet(), customer); } }
With Postman we can test the service:

Here the project of the example on github.