4.11.3. Base64 エンコーダーが JSON オブジェクトおよびアレイの Base64URL に更新
Eclipse Vert.x JSON タイプは RFC-7493 を実装します。以前のリリースの Eclipse Vert.x では、実装は Base64URL ではなく Base64 エンコーダーが誤って使用されていました。これは Eclipse Vert.x 4 で修正され、JSON タイプで Base64URL エンコーダーが使用されるようになりました。
Eclipse Vert.x 4 で Base64 エンコーダーを引き続き使用する場合は、設定フラグの legacy
を使用できます。以下の例は、Eclipse Vert.x 4 で設定フラグを設定する方法を示しています。
java -Dvertx.json.base64=legacy ...
アプリケーションを部分的に移行している場合は、Eclipse Vert.x 3.x から Eclipse Vert.x 4 への移行中に、バージョン 3 と 4 の両方にアプリケーションが必要になります。Eclipse Vert.x のバージョンが 2 つある場合は、次のユーティリティーを使用して Base64 文字列を Base64URL に変換できます。
public String toBase64(String base64Url) { return base64Url .replace('+', '-') .replace('/', '_'); } public String toBase64Url(String base64) { return base64 .replace('-', '+') .replace('_', '/'); }
以下のシナリオでは、ユーティリティーメソッドを使用する必要があります。
- Eclipse Vert.x 3.x リリースから Eclipse Vert.x 4 への移行中に統合の処理。
- Base64 文字列を使用する他のシステムとの相互運用性の処理。
以下のコード例を使用して、Base64URL を Base64 エンコーダーに変換します。
String base64url = someJsonObject.getString("base64encodedElement") String base64 = toBase64(base64url);
To Base64 および
のヘルパー関数は、JSON の移行だけを有効にします。オブジェクトマッピングを使用して JSON オブジェクトをアプリケーションの Java POJO に自動的にマッピングする場合、Base64 文字列を Base64URL に変換するためにカスタムオブジェクトマッパーを作成する必要があります。
toBase64
Url
以下の例は、カスタム Base64 デコーダーでオブジェクトマッパーを作成する方法を示しています。
// simple deserializer from Base64 to byte[] class ByteArrayDeserializer extends JsonDeserializer<byte[]> { ByteArrayDeserializer() { } public byte[] deserialize(JsonParser p, DeserializationContext ctxt) { String text = p.getText(); return Base64.getDecoder() .decode(text); } } // ... ObjectMapper mapper = new ObjectMapper(); // create a custom module to address the Base64 decoding SimpleModule module = new SimpleModule(); module.addDeserializer(byte[].class, new ByteArrayDeserializer()); mapper.registerModule(module); // JSON to POJO with custom deserializer mapper.readValue(json, MyClass.class);