You can encode and decode to and from base64 using ObjectMapper
and DatatypeConverter
specially for your use case where your class does not implements Serializable
.
public static String encode(AccessClient object) throws IOException{
ObjectMapper mapper = new ObjectMapper();
return DatatypeConverter.printBase64Binary(mapper.writeValueAsBytes(object));
}
public static AccessClient decode(String base64) throws IOException{
byte [] bytes = DatatypeConverter.parseBase64Binary(base64);
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(bytes,AccessClient.class);
}
Test Results :
public static void main(String [] args) throws IOException {
AccessClient accessClient = new AccessClient();
accessClient.setId("id");
accessClient.setClientId(new BigInteger("200000"));
accessClient.setClientName("clientName");
String base64 = encode(accessClient);
System.out.println(base64);
System.out.println(decode(base64).toString());
}
-----------------------Output--------------------------
eyJpZCI6ImlkIiwiY2xpZW50SWQiOjIwMDAwMCwiY2xpZW50TmFtZSI6ImNsaWVudE5hbWUiLCJjbGllbnRBdHRyaWJ1dGVzIjpudWxsfQ==
AccessClient{id='id', clientId=200000, clientName='clientName', clientAttributes=null}