Вы можете использовать следующее:
Сериализатор
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Base64;
public class Base64Serializer<T extends Serializable> extends StdSerializer<T> {
private static final long serialVersionUID = 1L;
protected Base64Serializer(Class<?> t, boolean f) {
super(t, f);
}
@Override
public void serialize(T value, JsonGenerator jsonGenerator, SerializerProvider arg2) throws IOException {
String ecnodedOutput = Base64.getEncoder().encodeToString(serialize(value));
jsonGenerator.writeString(ecnodedOutput);
}
public static byte[] serialize(Object obj) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(out);
os.writeObject(obj);
return out.toByteArray();
}
}
Регистрация и тестирование :
public class SerializerTest {
public static void main(String[] args) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule module = new SimpleModule();
addSerializers(module, Serializable.class, int.class, double.class, float.class, char.class, byte.class, short.class);
objectMapper.registerModule(module);
System.out.println(objectMapper.writeValueAsString(new A(10, "test", Arrays.asList(10000L, "TTTT2"))));
}
private static void addSerializers(SimpleModule module, Class... classes) {
Arrays.stream(classes).forEach(c -> module.addSerializer(new Base64Serializer(c, false)));
}
}
Вывод :
{
"id": "rO0ABXNyABFqYXZhLmxhbmcuSW50ZWdlchLioKT3gYc4AgABSQAFdmFsdWV4cgAQamF2YS5sYW5nLk51bWJlcoaslR0LlOCLAgAAeHAAAAAK",
"name": "rO0ABXQABHRlc3Q=",
"database": "rO0ABXNyABpqYXZhLnV0aWwuQXJyYXlzJEFycmF5TGlzdNmkPL7NiAbSAgABWwABYXQAE1tMamF2YS9sYW5nL09iamVjdDt4cHVyABdbTGphdmEuaW8uU2VyaWFsaXphYmxlO67QCaxT1+1JAgAAeHAAAAACc3IADmphdmEubGFuZy5Mb25nO4vkkMyPI98CAAFKAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cAAAAAAAACcQdAAFVFRUVDI="
}