MT Builder (Java to FIN MT)

Functionality to create a new SWIFT message by populating Java objects, and writing the result in SWIFT FIN format

The creation of a new message basically consists of creating an MT object for the specific message type, and the subsequent addition of fields in an orderly manner.

The order of the fields is very important and should be done accordingly to the standard, the model does not prevent creating incorrect content (Standard compliance validation can be checked with Prowide Integrator).

The following sections provides explanation by examples, creating messages in different common scenarios:

Creating a message with MT classes

The following example shows how to create a new MT103 message using the MT and Field helper classes. This is the easiest and recommended way to create a new message from scratch because the API will automatically fill mandatory header information with proper default values.

The first step is to create the specific MT object and set its general attributes:

final MT103 m = new MT103();

In the above code you can use either a full logical terminal address as parameter (12 characters) or just a plain BIC code (8 or 11 characters)

At this point all message blocks are initialized, and by default the message will be an outgoing message (input to SWIFT) with normal priority.

The next step is adding the message fields. For simple fields the final value can be directly provided as a single String value, and for more complex fields with several subfields (components) the Field helper classes may be used.

m.addField(new Field20("REFERENCE"));
m.addField(new Field23B("CRED"));

Field32A f32A = new Field32A()

Field50A f50A = new Field50A()

Field59 f59 = new Field59()
.setNameAndAddress("JOE DOE");

m.addField(new Field71A("OUR"));

Finally the message() method converts the object into its SWIFT representation:


The output of the above example is:


Notice for instance how field 50A was serializing into a proper value including the slash prefix for the account number and the line feed between the account and the BIC code.

/12345678901234567890 [CRLF]

The same procedure is used to create any type of message, changing the MT103 by any other available implementation.

Creating inner sequences

For more complex messages where fields must be contained in sequences (or sub blocks) some helper API is also provided by the message model. The following example shows how to append a General Information sequence A to an MT542:

SequenceA A = MT542.SequenceA.newInstance(

For boundary based sequences the opening and closing fields 16R and 16S will be automatically included.

When the message is converted to FIN, the above sample code will generate the following SWIFT content:


Notice sequences should be appended in the correct order, as well as fields inside the sequence block. The API will not control the correctness of the content being generated. The main reason to use this helper sequences API is the constraint provided in the MTnnn classes where only the proper SequenceX inner classes will exist, this way you can rest assure that the appended sequences are part of the specific message type.

Nested sequences can be also created with a similar API. The following example creates a complete MT517 with nested sequences A and A1:

MT517 mt = new MT517().append(MT517.SequenceA.newInstance(new SwiftTagListBlock()

Notice how in order to append both Field and sequence objects to the container A sequence, we create a generic SwiftTagListBlock on the fly. The created FIN content for the above snippet is:


Creating a message with specific header content

To create headers with special information, the block's API can be accessed directly. The following example illustrates how to create a message using the low level model layer to create specific headers, overriding the ones created by default in the MT103 constructor.

MT103 m = new MT103();

SwiftBlock1 b1 = new SwiftBlock1();

SwiftBlock2Input b2 = new SwiftBlock2Input();

m.getSwiftMessage().setBlock3(new SwiftBlock3());

In the above example a new and empty message object is created. The basic header (block 1) information is set with specific information, the application header (block 2) is created directly from a suitable string value with all its corresponding attributes pre set, and final an optional user header (block 3) is constructed by adding its tags.

Since the MTnnn classes uses a SwiftMessage instance inside, both level API can be combined; the MTnnn and Fieldnnn classes can be use to create the main content of the message and then the SwiftMessage object can be accessed to add or change specific header or trailer information.

Adding a message trailer block

In most situations there is no need to create the trailer block because it will be added automatically by the FIN interface. However, the API allows the creation of the trailer block as well as any other optional message block as follows:

MT103 m = new MT103();
SwiftBlock5 block5 = new SwiftBlock5();
block5.addTag(new Tag("MAC", "00000000"));
block5.addTag(new Tag("PDE", ""));

The resulting block when the object is serialized to FIN will be:


Adding optional blocks

Analogously to the description for Block 5, the same API can be used to create the optional User Header Block (block 3) or to add additional trailers.

In particular for the block 3, since it is becoming more used a new SwiftBlockBuilder helper class was introduced to ensure only valid fields are added to the block, and that fields are added in proper order.

For backward compatibility we have kept the SwiftBlock3 structure which is a List but the new builder provides specific setters for each available block 3 field. The builder instance is created directly from the block and works as a decorator on the underlying list of tags.

SwiftBlock3 b = new SwiftBlock3 ();
  .setField121(new Field121("value121"))
  .setField106(new Field106("value106"))
  .setField165(new Field165("value165"))
  .setField106(new Field106("finalValue106"))
  .setField108(new Field108("value108"));

Regardless of the setField calls order or repetition, the internal block 3 will be always consistent, and in the above example will produce this output:


The same applies when the message is created with an MT class. For instance if you need to add the MUR field 108 you can use the following:

MT103_STP mt = new MT103_STP(sender, receiver);

And the output will be:


Notice, fields 119 and 121 were automatically added when the MT instance was created, and the manually added field 108 was placed in the correct position in the existing block 3.

Finally this examples appends an optional application trailer block:

SwiftBlockUser blockUser = new SwiftBlockUser("S");
blockUser.addTag(new Tag("SAC", ""));
blockUser.addTag(new Tag("COP", "P"));