Base64 encoding in Vertx 4

358 views
Skip to first unread message

Mansi Agrawal

unread,
Jun 18, 2021, 1:59:43 AM6/18/21
to vert.x
Hi,
I am migrating my application from vertx 3 to vertx 4.
My application uses base64 encoding, while vertx now supports base64url, So I was following the given documentation.
https://vert-x3.github.io/vertx-4-migration-guide/index.html#changes-in-json_changes-in-common-components

Use the following example code to convert a Base64URL to Base64 encoder.

String base64url = someJsonObject.getString("base64encodedElement")
String base64 = Json.toBase64(base64url);

But I couldn't find the Json.toBase64() method in vertx4 API.

Mansi Agrawal

unread,
Jun 18, 2021, 6:18:34 AM6/18/21
to vert.x
Also, My issue is, that I am sending a byte array as a post request to my application.
So, I am decoding it in this way -
Someclass objName = Json.decodeValue(routingContext.getBodyAsString(), Someclass.class);
But this gives me the following error -
Failed to decode:Expected a base64 encoded byte array at .. 
Is there any way to fix this problem without using configuration flag ?

Paulo Lopes

unread,
Jun 22, 2021, 3:23:31 AM6/22/21
to vert.x

Hi,

Sadly, you cannot change the Base64 encoder on a request basis. The decoder follows the IJSON RFC, which states that binary should be base64url (and will follow the system config to fall back to the old encoding if legacy flag is on).

The goal in 4.0 was to make vert.x JSON web “stardards” compliant, if you really need a custom format for specific messages, then I’d suggest creating your instance of an “ObjectMapper”.

ObjectMapper mapper = new ObjectMapper();

SimpleModule module = new SimpleModule();

module.addDeserializer(byte[].class, new ByteArrayDeserializer());    // here you can specify how the Base64 string should be parsed

mapper.registerModule(module);

mapper.readValue(json, MyClass.class);

Mansi Agrawal

unread,
Jun 23, 2021, 1:32:13 AM6/23/21
to vert.x
Thanks for the update Paulo.
Reply all
Reply to author
Forward
0 new messages